這看起來像模型綁定導致我的問題。Asp.Net MVC - 將參數綁定到模型值!
基本上我有一個稱爲ProductOption模型和用於此問題它有2個字段
ID的目的(INT)PK 的ProductID(INT)FK
我有一個標準路線建立
context.MapRoute(
"Product_default",
"Product/{controller}/{action}/{id}",
new { controller = "Product", action = "Index", id = UrlParameter.Optional }
);
,如果用戶想增加一個選項的URL是,
/產品/選項/在上面的URL 1添加/ 1
是產品ID,我有以下的代碼返回一個空模型視圖,
[HttpGet]
public ActionResult Add(int id)
{
return View("Manage", new ProductOptionModel() { ProductID = id });
}
現在在我看來,我把一個隱藏字段
<%= Html.HiddenFor(x=>x.ID) %>
這是用來確定(上提交),如果我們在編輯或添加新的選項。然而,.net中的模型聯編程序似乎用1(或URL中的id參數的值)代替.ID(當離開上述get actionresult時爲0)
我該如何停止或解決此問題?
視圖模型
public class ProductExtraModel
{
//Database
public int ID { get; set; }
public string Name { get; set; }
public int ProductID { get; set; }
public ProductModel Product { get; set; }
}
你的代碼暗示它在模型中被稱爲ProductID而不是.ID? – Andrew 2010-06-14 15:11:34
你可以發佈視圖模型代碼嗎? – Andrew 2010-06-14 15:12:33
@SocialAddict,完成。 – LiamB 2010-06-14 15:14:25