Search code examples
asp.net-mvcautomapperspark-view-engine

Getting at ViewData.Model from spark template post


I am trying to convert my spark views to use ViewData.Model instead of the namevaluecollection so that I can use AutoMapper to map my dto's to entities before it gets into my action method.

I can access the viewdata.model from the view, but upon posting back the data, viewdata.model is null. here is some sample code:

in my view: <viewdata Message="string" model="MyDto" /> ${Model.Id} < -- displays MyDto.Id

In my filter on the server I am trying to do: var model = filterContext.Controller.ViewData.Model;

but ViewData.Model is null. This is during OnActionExecuted. Is there a trick to get the ViewData.Model to grab the values from the posted view?


Solution

  • This has nothing to do with Spark or AutoMapper. You need to learn MVC model binders (for example here).

    public ActionResult Action(MyDto dto)
    {
       // here dto is filled with values - automatically - if you have corresponding input fields
    }