运行以下代码时出现对象引用错误。我想选中复选框以更新数据库表。
@Html.CheckBoxFor(modelItem => item.ShippingDetail.Shiped, new { @id = item.ShippingDetail.ShippingDetailsID, @class = "chks"});
</tr>
}
}
<script type="text/javascript">
$(function () {
$(document).on("click", "input.chks", function (e) {
var _this = $(this);
var isChecked = _this.is(':checked');
$.post("@Url.Action("Update","Home")?id=" + _this.attr("id") +
"&newValue=" + isChecked, function (data) {
// do something with the response
});
});
});
控制器:
[HttpPost]
public ActionResult Update(int id, bool newValue)
{
//do your saving here.
return Json(true);
}
在模型中,我包含了一个已发货的列,该列在表类型中设置为位。目前,所有条目都设置为 false。同样在复选框上,似乎没有达到 javascript。
public partial class ShippingDetail
{
public ShippingDetail()
{
this.UserProfiles = new HashSet<UserProfile>();
}
public int ShippingDetailsID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Town { get; set; }
public string County { get; set; }
public string Country { get; set; }
public string PostCode { get; set; }
public Boolean Shiped { get; set; }
public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
}