I’m building a web-application with MS MVC 3 and have run into an issue, probably due to a hole in my understanding of model-binding.
Firstly, I have a fairly standard model (irrelevant stuff omitted for brevity, names changed to protect the privacy of innocent objects):
public class ModelBase
{
public int Id { get; set; }
}
public class Order : ModelBase
{
public List<Product> Products { get; set; }
}
public class Product : ModelBase
{
public int OrderId { get; set;}
}
For displaying and editing these, I have a View strongly typed to the Order class, containing a Partial View which is strongly typed to the Product class. The top of the partial view looks like this:
@model Product
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.OrderId)
//loads of actual editable properties
I insert the partial into the main view like this:
@Html.Partial("EditorTemplates/Product", Model.Products.First())
…and when the view is rendered in the browser, the “Id” hidden input contains the Id of the Order, not the Id of the Product that I expect and need 🙁
What am I missing? Can it be fixed without changing the structure of the model and views?
On changing my test project I can confirm that
/Home/Index?id=33
or
/Home/Index/33
DOES overwrite the model value. Can you remove this parameter from the url?