I am trying to pass an object from a cshtml view to JQuery, cshtml code:
foreach (var item in Model)
{
<tr class="link" data-graduate="@item" data-url="@Url.Action("PopulateModal","CMIPGraduate")">
<td>@Html.DisplayFor(display => item.State)</td>
<td>@Html.DisplayFor(display => item.Name)</td>
</tr>
}
JQuery code:
$(document).ready(function () {
$(".link").click(function () {
alert($(".link").data("graduate"))
$.ajax({
type: "get",
url: $(".link").data("url"),
data:{graduate:$(".link").data("graduate")}
})
$("#myModal").modal("show");
})
})
The object is always null when it arrives to the Controller, I am trying to display it with an alert
in my JQuery
code as you see but all I am getting is something like this: MyProject.Viewmodels.MyClassVM
, I've been trying to use alert(JSON.stringify($(".link").data("graduate")))
but I am getting the same results. I know that in my cshtml view I could do something like this:
<tr class="link" data-graduate="@item.Name" data-url="@Url.Action("PopulateModal","CMIPGraduate")">
and I will get the name easily on my JQuery
function, but I don't want to create a data-
attribute in my cshtml view by field, what if I have 500 fielsd? I would prefer to get the whole object and send it to my controller action.
Just in case this is my Controller action:
public ActionResult PopulateModal(MyClassVM graduate)
{
return PartialView(graduate);
}