I have a strongly-typed partial view whose model contains a property with the same name as the parent page's view model. For some reason the rendering engine is rendering the parent view model value, not the expected value (well, the value I expect at least!)
Parent page view model extract:
public class ParentPageViewModel
{
public int Id { get; set; } // problem property
...
public IEnumerable<ChildViewModel> Children { get; set; }
}
Child page view model extract:
public class ChildViewModel
{
public int Id { get; set; } // problem property
...
}
Parent page extract (Razor):
@model ParentPageViewModel
...
@foreach (var item in Model.Children)
{
@Html.Partial("MyPartialView", item)
}
...
Partial view extract:
@model ChildViewModel
...
<form ...>
@Html.HiddenFor(m => m.Id) // problem here - get ParentPageViewModel.ID not ChildViewModel.Id
</form>
...
So basically in my rendered output, my hidden field has the value of the parent view model element, NOT the value passed to the partial view. It's definitely being caused by the name, as changing the name of @ChildViewModel.Id@ to something like @ChildViewModel.ChildId@ makes it work as expected. Interestingly, when inspecting the view model values in the debugger I do see the correct values; it's only the rendered output that's wrong.
Is there a way round this or 'correct' way of doing what I'm trying to do (I'm rendering mini forms in a table for ajax validation/posting of updates to table rows)
Thanks,
Tim