Bobscure Bobscure - 26 days ago 13
C# Question

MVC builds html page but does not show it

I have a problem concerning Asp.net MVC.

I have an Index.cshtml page with the following javascript

$('#employeeTable tbody').on('dblclick', 'tr', function() {
var mitarbeiterId = table.row(this).data().Id;
$.post('@Url.Action("IndexCompletion")', { id: mitarbeiterId });
});


This basically just gets me the id of an employee and calls this ActionResult in my Controller

[HttpPost]
public ActionResult IndexCompletion(int id)
{
Mitarbeiter ma = new LeistungserfassungService.LeistungserfassungService().GetMitarbeiterById(id);
return View("IndexCompletion", new IndexCompletionViewModel{Mitarbeiter = ma});
}


Now i hoped that this will show me the following page:

@model Leistungserfassung.Models.IndexCompletionViewModel

@{
ViewBag.Title = "Completion";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="content">
<h2>Completion</h2>
@Model.Mitarbeiter.Nachname
</div>


But what happens now is, it builds the page successfully, as i can see on the networking tab in Google Chromes dev tools, but does not redirect to it.
Also when i directly try to navigate to this .cshtml file, the browser tells me it can not be found.

Does anyone have an idea of what could be the problem on this?
I am very new to javascript so i am grateful for any advice!

Thanks in advance and sorry for the german parts in my code.

Answer

You're making an async ajax call to the server using JavaScript this means JavaScript needs to redirect the user to another page. So, in order for this to happen your controller post method should return a response to JavaScript (most likely JSON) which will have a redirect link (key, value) item to then redirect.

Comments