我在這裏將數據從視圖傳遞到控制器的問題很少。這是一個小的CRUD網站,這裏有一些代碼來解釋我的問題。MVC將數據從視圖傳遞到cotroller
Model:
public class TempModel{
public List<Temp> Temp1 { get; set; }
}
View:
@{ using (Html.BeginForm()) {
for (int i = 0; i < TempModel.Temp1.Count; i++)
{
@Html.LabelFor(model => model.Temp[i].ModifiedBy, "Modified By: ")
@Html.TextBoxFor(model => model.Temp[i].ModifiedBy)
<button name="button" value="update">Update</button>
}
}
Controller:
[HttpPost]
public ActionResult Index(TempModel.Temp1, string button){
switch (button)
{
case "delete":
break;
case "update":
UpdateSingleTemp(TempModel.Temp1);
break;
}
}
現在我想要做的就是傳遞哪些Temp1 [i]對象在用戶按下更新時被更改。 必須有簡單的方法來告訴控制器ItemId或dataIsChanged或類似的東西,但我現在無法弄清楚。我很新的MVC。
編輯: 我設法解決這個問題..最簡單的方法就是使用多個表單並從模型中刪除列表。
@for (int i = 0; i < TempModel.Count; i++)
@{ using (Html.BeginForm()) {
{
@Html.LabelFor(model => model[i].ModifiedBy, "Modified By: ")
@Html.TextBoxFor(model => model[i].ModifiedBy)
<button name="button" value="update">Update</button>
}
}