I am a little new to microsofts MVC framework and seem to be stuck on this issue.
I am trying to post an id which is in json format to my controller action, the controller returns a view with a model, so I am not returning any response back for a success callback to handle the data. The call does not need to be an ajax call, I want to display a brand new view from my click event on my table row.
My code is as follows:
Jquery, added a click event on my table body rows, when I click on a row, I get the id of the row and post to a specific controller action (/Document/Details)
$(document).ready(function () {
$("#tbdocuments").on("click", "tbody tr", function (e) {
var id = $(this).attr("data-id");
console.log(id);
$.post("/Document/Details", { "id": id })
});
})
Mvc Controller:
The controller successfully receives the id passed from the client( the table row click), and I fetch my model correctly to pass to the view.
public ActionResult Details(string id)
{
var model = documentHelper.FetchSingleDocument(Guid.Parse(id));
return View(model);
}
Razor View Details.cshtml:
This is the view that my markup is on.
@model DocumentBuddy.Mvc.ViewModels.DocumentViewModel
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_LayoutSecure.cshtml";
}
<h2>Details</h2>
<div>
<h4>DocumentViewModel</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.DocumentId)
</dt>
<dd>
@Html.DisplayFor(model => model.DocumentId)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd>
@Html.DisplayFor(model => model.Description)
</dd>
<dt>
@Html.DisplayNameFor(model => model.MimeType)
</dt>
<dd>
@Html.DisplayFor(model => model.MimeType)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Length)
</dt>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
@Html.ActionLink("Back to List", "Index")
</p>
After I click on my table row, it fires the controller and it fetch the model, however the Details view is never ever display, and it still stays on the same view. I did inspect the page as well with no errors been displayed. I am quite confused on how to do this in MVC without it been an ajax call, I need to redirect to a new view. Any help would be greatly appreciated. Thanks.