I have a controller HomeController with the following action method:
[HttpPost]
public ActionResult DisplayData(MyViewModel myViewModel)
{
// Do something with myViewModel
}
The ViewModel:
public class MyViewModel
{
public string Name { get; set; }
public string Surname { get; set; }
public bool IsPeriod { get; set; }
}
And the following View
@model AppName.ViewModels.MyViewModel
@{ Html.RenderPartial("MyPartialView", Model); }
<img src="@Url.Action("DisplayData", "Home", new { myViewModel = Model })" alt="Image" />
I use the Url.Action how it is described here but what I get in the DisplayData action method is null. In the source code I got:
<img src="/Home/DisplayData?filters=AppName.ViewModels.MyViewModel" alt="Image" />
so it is passing actually the type instead of the values.
The ViewModel instead is correctly passed to the partial view. What am I doing wrong?
I usually add the following to my model:
Then you can simply use the RouteValues property in your Url.Action() call.
Or if your prefer less (explicit) code, ignore the model changes and simply do this: