Bobscure Bobscure - 4 months ago 28
C# Question

MVC builds html page but does not show it

I have a problem concerning 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

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">

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.


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.