In my MVC application I have a View Model that looks similar to this:
public class ComplexViewModel
{
public ComplexDetailsViewModel Details1 { get; set; }
public ComplexDetailsViewModel Details2 { get; set; }
}
public class ComplexDetailsViewModel
{
public int Id { get; set; }
public string DisplayValue1 { get; set; }
public string DisplayValue2 { get; set; }
// ...
}
I was originally doing the following in my view:
@Html.HiddenFor(model => model.Details1.Id)
@Html.HiddenFor(model => model.Details2.Id)
@Html.DisplayFor(model => model.Details1.DisplayValue1)
...
I would POST the full model to the controller:
public ActionResult Post(ComplexViewModel model)
I don't actually need anything from ComplexViewModel except for the Id values, so I decided to create another view model used specifically for POSTing the data:
public class PostViewModel
{
public int Details1Id { get; set; }
public int Details2Id { get; set; }
}
public ActionResult Post(PostViewModel model)
The problem is that now my @HiddenFor(model => model.Details1.Id)
does not map to my POST model so nothing actually gets POSTed.
Is there a way to have the separate structure for my POST model and my GET model while still using the HiddenFor
helper?