views:

44

answers:

1

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.

+1  A: 

There is no way that MVC is just picking up a variable that you haven't explicitely passed to your view or have hanging around in Session or TempData.

Since your setting that Id to -1 I'm start there for problems.

The other possibility is that -1 is hanging around in your ModelState someplace. The html helpers look to ModelState first before deciding to use any values you pass in.

jfar
It does seem to be coming from the ModelState. Reading around it seems I have to not use HTML Helpers for properties on the model that have changed since being posted tom the Controller. In this case that will be just the Id property. Does that make sense ?
andynormancx
@andynormancx, yeah, the ModelState greedy HtmlHelpers bite everybody it seems.
jfar
It seems a very odd way for it to work.
andynormancx
If this is by design that would be really depressing! Have you figured out what going on or are we actually supposed to make 2 trips to the db in order to get auto generated id, or query modelstate directly if we don't want to do that?
Erx_VB.NExT.Coder
Just to add I remember my id field wasn't being updated in mvc and I had to make another trip to get it so had the same problem as you did.
Erx_VB.NExT.Coder