我正在使用ASP.NET MVC 4,但我不認爲這個問題的目的是重要的。如何在ASP.NET MVC中編輯列表時保留輸入ID?
我的編輯視圖有一個相對複雜的模型。就像這樣:
public class Recipe_model
{
public string Name { get; set; }
public List<Recipe_Ingredient_model> Ingredients { get; set; }
}
其中主料是
public class Recipe_Ingredient_model
{
public int RecipeID { get; set; }
public int? UnitID { get; set; }
public double? Quantity { get; set; }
public Ingredient_model Ingredient { get; set; }
}
它本身包含的成分模型。
當我爲此創建表單時,內置Html.EditorFor()
不適用於任何超過Recipe_model
屬性的內容,因此我使用部分視圖來顯示每個子模型的編輯器。
這工作正常儘可能的接口出現,但是當我提交表單到控制器,並嘗試綁定到Recipe_model
使用自動
[HttpPost]
public ActionResult Edit(Recipe_model model)
{
return View(model);
}
它失敗,因爲輸入元素的部分的ID視圖不符合正確的模式(我認爲ParentModel_Property)。
通過在控制器中對部分視圖中的ID進行硬編碼或從FormCollection
手動進行綁定的簡稱,是否有某種方法可以在部分視圖中獲取正確的ID,以便模型在提交時自動綁定?
你可以發佈你的部分視圖的代碼? – nemesv