我正在尝试将复选框信息发布回控制器,以便可以将更新数据保存在数据库中。当前,我具有页面上的所有值,但是当我选中或取消选中该框时,它永远不会进入控制器。
控制器
[HttpPost]
public void SaveSettings(SettingsListModel SettingsModel)
{
DataLayer dl = new DataLayer();
}
查看
@model WebApplication1.Models.SettingsListModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="@Url.Content("~/Scripts/app.js")"></script>
<title>Settings</title>
</head>
<body>
<h1>Settings</h1>
<div>
<form asp-action="SaveSettings" asp-controller="Home" method="post">
<div align="center">
<div align="center">
@for (int i = 0; i < Model.listOfSettings.Count; i++)
{
@Html.HiddenFor(m => m.listOfSettings.ElementAt(i).settingID)
@Html.HiddenFor(m => m.listOfSettings.ElementAt(i).settingText)
<div>
@Html.DisplayTextFor(m => m.listOfSettings.ElementAt(i).settingText)
</div>
<div>
//@Html.HiddenFor(m => m.listOfSettings.ElementAt(i).settingsSelected)
@Html.CheckBoxFor(m => m.listOfSettings.ElementAt(i).settingsSelected)
</div>
<br />
<br />
<br />
}
</div>
</div>
</form>
</div>
</body>
</html>
型号
public class SettingsListModel
{
public List<SettingsModel> listOfSettings { get; set; }
}
public class SettingsModel
{
public int settingID { get; set; }
public string settingText { get; set; }
public bool settingsSelected { get; set; }
}
答案 0 :(得分:0)
您不能在表达式中使用.ElementAt()
,因为它会生成name
与模型无关的属性。由于该属性为List<T>
,因此只需使用集合索引器
@for (int i = 0; i < Model.listOfSettings.Count; i++)
{
@Html.HiddenFor(m => m.listOfSettings[i].settingID)
@Html.HiddenFor(m => m.listOfSettings[i].settingText)
<div>
@Html.DisplayTextFor(m => m.listOfSettings[i].settingText)
</div>
<div>
@Html.CheckBoxFor(m => m.listOfSettings[i].settingsSelected)
</div>
}
您的<form>
元素也缺少将值发布到控制器的“提交”按钮。
答案 1 :(得分:0)
Enumerable.ElementAt()
返回一个类型(其返回类型为TSource
),而不是必须在name
属性中添加的属性名称。改为使用集合属性索引器:
<form asp-action="SaveSettings" asp-controller="Home" method="post">
<div align="center">
<div align="center">
@for (int i = 0; i < Model.listOfSettings.Count; i++)
{
@Html.HiddenFor(m => m.listOfSettings[i].settingID)
@Html.HiddenFor(m => m.listOfSettings[i].settingText)
<div>
@Html.DisplayTextFor(m => m.listOfSettings[i].settingText)
</div>
<div>
@Html.CheckBoxFor(m => m.listOfSettings[i].settingsSelected)
</div>
<br />
<br />
<br />
}
</div>
</div>
<!-- add a button here to submit the form -->
<button type="submit">Submit</button>
</form>
标记帮助程序版本:
<form asp-action="SaveSettings" asp-controller="Home" method="post">
<div align="center">
<div align="center">
@for (int i = 0; i < Model.listOfSettings.Count; i++)
{
<input asp-for="listOfSettings[i].settingID" type="hidden" />
<input asp-for="listOfSettings[i].settingText" type="hidden" />
<div>
@Html.DisplayTextFor(m => m.listOfSettings[i].settingText)
</div>
<div>
<input asp-for="listOfSettings[i].settingsSelected" type="checkbox" />
</div>
<br />
<br />
<br />
}
</div>
</div>
<!-- add a button here to submit the form -->
<button type="submit">Submit</button>
</form>
此外,您需要将返回类型从void
替换为ActionResult
(如果使用.NET Core,则应替换为IActionResult
):
[HttpPost]
public ActionResult SaveSettings(SettingsListModel SettingsModel)
{
DataLayer dl = new DataLayer();
// do something
return View(model);
}