I have an ASP.NET MVC View. In my View I want to make an AJAX Post request to update a PartialView inside the view.
This is my Model:
public class HomeListViewModel
{
public MainQueryViewModel MainQuery { get; set; }
// other properties
}
And this is my View (which has a PartialView):
@model MyNameSpace.HomeListViewModel
@using (Ajax.BeginForm("List", new AjaxOptions { UpdateTargetId = "browsePartialView", HttpMethod = "Post" }))
{
@Html.AntiForgeryToken()
<div id="browsePartialView">
@Html.Partial("_MainQuery", Model.MainQuery)
</div>
}
Now this is my problem: I want AJAX to POST an object of type: HomeListViewModel
to the controller, but AJAX Posts an object of type MainQuery
.
This is my Controller Action:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult List(HomeListViewModel HomeListViewModel)
{
var myModel = HomeListViewModel; // <-- an empty model is passed in
// some action
}
If I change the param type to: MainQuery
, then it works fine:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult List(MainQuery MainQuery)
{
var myModel = MainQuery; // <-- all ok
// some action
}
Is there a way to tell AJAX what type of Model it should post to Controller?