我的模型複選框值未通過POST提交
public class User
{
public string Id { get; set; }
[Display(Name = "")]
public bool IsSelected { get; set; }
[Display(Name = "Nom")]
public string Name { get; set; }
[Display(Name = "Prénom")]
public string Firstname { get; set; }
}
public class ListUser
{
public string ClickedButton { get; set; }
[Display(Name = "")]
public List<Models.User> Users;
}
index.cshtml
在此頁面上顯示我從所有的ListUser
用戶,每個用戶可選擇一個複選框
@model Models.ListUser
<!-- some content here -->
<script>
function clickedButton(i){
document.getElementById("ClickedButton").value = i;
return true;
}
</script>
@using (Html.BeginForm("Index", "Users", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(m => m.ClickedButton);
for (var i = 0; i < Model.Users.Count(); i++)
{
@Html.CheckBoxFor(m => m.Users[i].IsSelected)
@Html.DisplayNameFor(m => m.Users[i].Name)
@Html.DisplayFor(m => m.Users[i].Name)
@Html.DisplayNameFor(m => m.Users[i].Firstname)
@Html.DisplayFor(m => m.Users[i].Firstname)
}
<input type="submit" value="Réinitialiser les mots de passe" class="btn btn-default" onclick="clickedButton('psw')"/>
}
這裏是控制器(當我提交fo時調用這個控制器RM)
[HttpPost]
public ActionResult Index(ListUser model)
{
// I added a breakpoint here
return this.RedirectToAction("Index", "Home");
}
我想所有選定的用戶發送到控制器,例如,當我選擇4個用戶,我想找回它們控制器:
的問題:當我提交只在表格中設置ClickedButton
字段,我選擇的用戶(通過複選框)未設置。
問題是什麼?
這是什麼?'的onclick =「clickedButton( 'PSW')' –
@BviLLe_Kid我忘了提,它設置隱藏的元素'@ Html.HiddenFor(M =>價值m.ClickedButton);''我會更新代碼 –