Javascript .Net Core单选按钮始终返回false
我正在向视图发送列表。 试图通过表单提交列表中的每个项目 型号:Javascript .Net Core单选按钮始终返回false,javascript,jquery,asp.net,.net,asp.net-mvc,Javascript,Jquery,Asp.net,.net,Asp.net Mvc,我正在向视图发送列表。 试图通过表单提交列表中的每个项目 型号: 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 As
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");
}
<script>
$(function () {
$('input[type=radio]').change(function () {
var data = JSON.stringify($(this).closest('form').serializeArray());
console.log(data);
$(this).closest('form').submit();
});
});
</script>
控制器:
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");
}
<script>
$(function () {
$('input[type=radio]').change(function () {
var data = JSON.stringify($(this).closest('form').serializeArray());
console.log(data);
$(this).closest('form').submit();
});
});
</script>
当我期望控制器中IsEnabled的值为真时,我得到的是假值
但是当表单序列化并显示在控制台中时,该值与预期一样为true
这是因为事件处理程序吗?无线电输入名称中的数组前缀(
[0].IsEnabled
)导致输入未映射到模型中,因为该名称应为IsEnabled
。
作为简单的解决方法,您可以在不使用HTML帮助程序的情况下手动创建输入,例如:
<input type="radio" name="IsEnabled" value="True"/> <label>True</label>
<input type="radio" name="IsEnabled" value="False"/> <label>False</label>
True
错误的
有关其他详细信息,请参见帮助器的