【发布时间】:2020-01-12 08:32:22
【问题描述】:
我正在向视图发送一个列表。 尝试通过表单提交列表中的每个项目。
型号:
public partial class Model
{
public int Id { get; set; }
public string UId { get; set; }
public int WId { get; set; }
public bool IsEnabled { get; set; }
public virtual AspNetUsers User { get; set; }
public virtual WModel W { get; set; }
}
控制器:
public IActionResult UWC()
{
List<Model> uW = db.UW.Include(x => x.U).Include(x=>x.W).ToList();
return View(uW);
}
[HttpPost]
public IActionResult UWC(Model uW)
{
var s = ModelState.ErrorCount;
return RedirectToAction("UWC");
}
查看
@model List<Model>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model[0].W.Name)
</th>
<th>
@Html.DisplayNameFor(model => model[0].W.Type)
</th>
<th>
@Html.DisplayNameFor(model => model[0].W.Description)
</th>
<th>
@Html.DisplayNameFor(model => model[0].W.IsActive)
</th>
</tr>
</thead>
<tbody>
@for (var item = 0; item < Model.Count(); item++)
{
<tr>
<td>
@Html.DisplayFor(model => model[item].W.Name)
</td>
<td>
@Html.DisplayFor(model => model[item].W.Type)
</td>
<td>
@Html.DisplayFor(model => model[item].W.Description)
</td>
<td>
<form asp-action="UserWidgetConfiguration" asp-controller="UserManagement" method="post">
@Html.RadioButtonFor(model => model[item].IsEnabled, true, new { @Name = "IsEnabled",, id = "enabled_" + item } })<label>True</label>
@Html.RadioButtonFor(model => model[item].IsEnabled, false, new { @Name = "IsEnabled" ,, id = "disabled_" + item }})<label>False</label>
<input type="hidden" asp-for="@Model[item].Id" value="@Model[item].Id" name="Id" />
<input type="hidden" asp-for="@Model[item].WidgetId" value="@Model[item].WidgetId" name="WidgetId" />
<input type="hidden" asp-for="@Model[item].UserId" value="@Model[item].UserId" name="UserId" />
</form>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
@section Scripts{
<script>
$(function () {
$('input[type=radio]').change(function () {
$(this).closest('form').submit();
});
});
</script>
}
以上是完整的代码。
单选按钮始终绑定为 false。
但是, 我正在使用脚本提交单选按钮值更改表单:
<script>
$(function () {
$('input[type=radio]').change(function () {
var data = JSON.stringify($(this).closest('form').serializeArray());
console.log(data);
$(this).closest('form').submit();
});
});
</script>
控制台会为单选按钮提供正确的值。
[{"name":"[0].IsEnabled","value":"True"},{"name":"Id","value":"1"},{"name":"WId","value":"1"},{"name":"UId","value":"a"},{"name":"__RequestVerificationToken","value":"CfDJ8FLfqW1-k2hNlQsGtRBQVqOKRWmlkQYWTMrEQNcJHWGd7_LPvHfjw3V5gxYAnLjwz7HvjExZDXgmbSMQ1DnEONg7EJfu5OqYm7xntqXIe4IMkD1HD4SHRtihdyzDXHrKu7jNXUwuN1B6H-565O0J0oZsGVCopZqy180oPco29vqyvpcZkZnWEOmVQX6_V3T6AQ"}]
当我期望控制器中 IsEnabled 的值为真时,我得到的是假值。
但是当表单被序列化并显示在控制台中时,该值如预期的那样为真。
这是因为事件处理程序吗?
【问题讨论】:
标签: javascript jquery asp.net .net asp.net-mvc