【发布时间】:2020-01-13 11:14:32
【问题描述】:
我试图了解为什么此绑定不起作用。
在我将类型从 struct 更改为 class 之前,绑定不起作用。
这是设计使然还是我遗漏了什么?
我正在使用 asp.net core 2.2 MVC
查看模型
不工作
public class SettingsUpdateModel
{
public DeviceSettingsStruct DeviceSettings { get; set; }
}
工作
public class SettingsUpdateModel
{
public DeviceSettingsClass DeviceSettings { get; set; }
}
public class DeviceSettingsClass
{
public bool OutOfScheduleAlert { get; set; }
// other fields removed for brevity
}
public struct DeviceSettingsStruct
{
public bool OutOfScheduleAlert { get; set; }
// other fields removed for brevity
}
控制器
[HttpPost]
public IActionResult Update(SettingsUpdateModel newSettings)
{
// newSettings.DeviceSettings.OutOfScheduleAlert always false on struct but correct on class
return Index(null);
}
查看
<input class="form-check-input" type="checkbox" id="out_of_schedule_checkbox" asp-for="DeviceSettings.OutOfScheduleAlert">
预期:DeviceSettings.OutOfScheduleAlert 绑定到与类相同的结构
实际:只绑定了类参数
【问题讨论】:
标签: asp.net-core asp.net-core-mvc model-binding