Shadouspan Shadouspan - 1 year ago 138
Javascript Question

Navigate to Another Page With JavaScript

I am new in MVC, when I click on a div with JavaScript, I want to go to the detail page that is relevant to that div. My JavaScript and controller code is as follows, and I get and error. Where do I make mistakes?

My JavaScript code:

<script>
$('div.ProjePartialGovde').click(function (el) {

var projeid = $(this).data('id');

$.ajax({
method: "get",
url: '@Url.Action("Detay", "Proje")',
data: { ID: projeid }
})
.done(function (msg) {
window.location.href = '@Url.Action("Detay", "Proje")' + "projeID=" + projeid;
})
});




And my controller.cs code:

public ActionResult Detay(int? ID)
{
var prj = (TamYetki || OzelKullanici) ? (db.tbl_Proje.FirstOrDefault(d => d.ID == ID)) : (db.tbl_Proje.FirstOrDefault(d => d.ID == ID && d.BirimID == kul.BirimID));

if (prj == null)
{
BilgiMesaji(Tur.Error, "No records found!");
return RedirectToAction("Index");
}
return View(prj);
}


Edit: I changed my JavaScript code like this:

<script>
$('div.ProjePartialGovde').click(function (el) {

var projeid = $(this).data('id');
alert(projeid);
(function(){
Html.ActionLink("Detay", "Proje", new { ID: projeid });
})
});



Answer Source

You don't need ajax to redirect to a page.

Change it to:

$('div.ProjePartialGovde').click(function (el) {
    var projeid = $(this).data('id');
    window.location.href = '@Url.Action("Detay", "Proje")' + "?ID=" + projeid;
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download