I am trying to work out whether I am misunderstanding something about ASP.NET MVC or whether I have found some sort of bug in ASP.NET MVC Beta 3. I am having a problem with a PartialView picking up the wrong model when using HTML Helper extensions
My controller code looks like this:
public ActionResult EditGeneral(MapGeneralViewModel vm)
{
var query = MapGeneralViewModel.ToModel(vm, svcMaps);
return PartialView("General", MapGeneralViewModel.FromModel(query));
}
In the case of this being an insert, the property vm.Id starts out as -1 and after the call to MapGeneralViewModel.ToModel it has been persisted the database and query.Id has a proper value.
The call to MapSettingsViewModel.FromModel returns a new viewmodel and I have checked that the Id property correct contains the newly created id value.
The relevant bits of the view look like:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AdminWebRole.Models.Map.MapGeneralViewModel>" %>
<%: Model.Id %>
<%= Html.Hidden("IdTest", Model.Id) %>
<%= Html.HiddenFor(model => model.Id) %>
If I put a breakpoint in the view, Model.Id is correctly set to the right value.
The actual output of the controller (when Model.Id == 70) looks like this:
70
<input id="IdTest" name="IdTest" type="hidden" value="-1" />
<input id="Id" name="Id" type="hidden" value="-1" />
So the value output without using the HTML helpers is correct, but the values output by the helpers somehow is picking up the viewmodel that was passed into the controller !
I have no idea how this is happening. I have tried various things:
- Using View() rather than PartialView()
- assigning the results of MapGeneralViewModel.FromModel() to vm and then passing vm to the view
- using <%: and <%=
- setting vm to null (the old view model somehow still gets used)
- changing the value of the incoming id to 0 (results in 0 being output in the view instead of -1)
- the problem isn't specific to properties called "Id", I have also tested other fields with the same result
Am I confused over how this is supposed to work or have I hit a beta bug ? If it makes any difference, this is running inside my local Azure runtime on a Win7 64 bit machine.