![](/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.