![](/img/trans.png)
[英]Jqwidget checkbox in grid cell is allways checked even if its value coming from Json is false
[英]Model property always get false value from a checkbox even if its check. data is json from ajax call
我有一个表单,提交时将调用以下代码:
$.ajax({
url: '/Company/CheckError',
type: 'POST',
data: JSON.stringify($(this).serializeObject()),
dataType: 'json',
processData: false,
contentType: 'application/json; charset=utf-8',
success: function (data) {
}
});
如果未选中我的IsActive复选框,我发现它返回以下json数据:
{"Email":"test@test.com","Name":"test","Phone":"","IsActive":"false","submitType":"","Id":"59"}
我觉得很自然。 但是如果选中了IsActive复选框,它将返回此json数据:
{"Email":"test@test.com","Name":"test","Phone":"","IsActive":["true","false"],"submitType":"","Id":"59"}
现在在我的控制器中,
public ActionResult Method(SomeModel model)
{
}
另一个属性绑定得很好。 但是模型.IsActive总是假的。 我认为MVC通过绑定真值而不是隐藏输入的值来正确处理这个问题。
我错过了什么吗? 预谢谢你的帮助=')
试试这样:
模型:
public class SomeModel
{
public bool IsActive { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new SomeModel();
return View(model);
}
[HttpPost]
public ActionResult Index(SomeModel model)
{
return Json(new { success = model.IsActive });
}
}
视图:
@model SomeModel
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(x => x.IsActive)
@Html.CheckBoxFor(x => x.IsActive)
</div>
<p><input type="submit" value="OK"></p>
}
<script type="text/javascript">
$('form').submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (data) {
alert(data.success);
}
});
return false;
});
</script>
将以下内容添加到控制器方法中:
model.IsActive = model.IsActive.Contains("true");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.