Asp.Net MVC - Binding of parameter to model value!
Posted
by Pino
on Stack Overflow
See other posts from Stack Overflow
or by Pino
Published on 2010-06-14T15:04:13Z
Indexed on
2010/06/14
19:32 UTC
Read the original article
Hit count: 258
This seems like the model binding is causing me issues.
Essentially I have a model called ProductOption and for the purpose of this question it has 2 fields
ID (Int) PK ProductID (Int) FK
I have a standard route set-up
context.MapRoute(
"Product_default",
"Product/{controller}/{action}/{id}",
new { controller = "Product", action = "Index", id = UrlParameter.Optional }
);
and if the user wants to add an option the URL is,
/Product/Options/Add/1
in the above URL 1 is the ProductID, I have the following code to return a blank model the the view,
[HttpGet]
public ActionResult Add(int id)
{
return View("Manage", new ProductOptionModel() { ProductID = id });
}
Now in my view I keep a hidden field
<%= Html.HiddenFor(x=>x.ID) %>
This is used to determine (on submit) if we are editing or adding a new option. However the Model binder in .net seems to replace .ID (Which was 0 when leaving the above get actionresult) with 1 (or the value of the id parameter in the URL)
How can I stop or work around this?
ViewModel
public class ProductExtraModel
{
//Database
public int ID { get; set; }
public string Name { get; set; }
public int ProductID { get; set; }
public ProductModel Product { get; set; }
}
© Stack Overflow or respective owner