I have a problem with an ASP.NET MVC page that shows a list of orders being dispatched to a particular location. The page is called Orders/DispatchToCentre/2 where 2 is the ID of the Centre from the database and it displays fine.
The trouble is that I want to be able to postback changes to the orders on the page, so I’m doing this:
<dl class="orders">
@foreach (OrderViewModel ovm in Model)
{
<dd>@ovm.User.Name </dd>
<dt>
@Html.Partial("OrderTable", ovm)
<p>
@using (Html.BeginForm("Dispatch", "Order", new { id = ovm.Id }, FormMethod.Post ) )
{
@Html.TextBox("id", ovm.Id );
@Html.Hidden("OriginatingPage", Request.Path);
<input type="submit" name="submit-@ovm.Id" value="Order dispatched" />
}
</p>
</dt>
}
</dl>
The problem is that when I look at the page source I get this:
<p>
<form action="/Order/Dispatch/3f180e5b-d112-4245-98ef-602e86eccda6" method="post">
<input id="id" name="id" type="text" value="2" />
<input id="OriginatingPage" name="OriginatingPage" type="hidden" value="/Order/DispatchToCentre/2" />
<input type="submit" name="submit-3f180e5b-d112-4245-98ef-602e86eccda6" value="Order dispatched" />
</form>
</p>
The id input appears to have totally ignored the OrderViewModel.Id that I am explicitly setting it to ( this should be a Guid ) and automatically set itself to use the Centre Id from the path instead.
Is there some configuration I need to use to avoid this bit of interfering automagicalness or am I best just to ignore the helper here and create the field manually? Obviously, the latter is a fairly trivial task but it would be helpful to understand if I am conceptually misunderstanding some important facet of the Razor rendering action or I’m just running into a quirk.
In controller,
HttpGetaction, before returning view, you should callFact is that
ModelStatehas higher priority in providing model values than the model itself, so value from ModelState is always preferred (if any) over model value