1 回答

TA貢獻1804條經驗 獲得超8個贊
將 Id 傳遞給您的 post 方法,控件名稱應與模型屬性名稱匹配,以將值綁定到 post 模型參數。迭代這些控件來為每個控件創(chuàng)建唯一的 ID。在表單發(fā)布中,它將數據綁定到列表模型。
看看我下面的代碼。
看法 :
@using (Html.BeginForm("UpdateRoles","AdminRoles",FormMethod.Post))
{
@for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td>
//to pass id to your post method
@Html.HiddenFor(m => m[i].Id)
@Html.DisplayFor(m => m[i].Name)
</td>
<td>
@Html.CheckBoxFor(m => m[i].IsChecked, new { })
</td>
</tr>
}
<input id="Submit" type="submit" value="submit" />
}
控制器 :
[HttpPost]
public ActionResult UpdateRoles(List<CompanyRole> model)
{
//your logic
return View();
}
模型 :
public class CompanyRole {
public int Id {get; set;}
public string Name {get; set;}
public bool IsChecked {get; set;}
}
- 1 回答
- 0 關注
- 148 瀏覽
添加回答
舉報