对象引用错误运行复选框

时间:2013-06-20 11:07:22

标签: asp.net-mvc

运行下面的代码时出现Object引用错误。我想检查复选框以更新数据库表。

        @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);
    }

在Model I中包含了一个shiped列,它在表类型中设置为bit。目前所有条目都设置为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; }
}

}

1 个答案:

答案 0 :(得分:0)

试试这个

<script type="text/javascript">
$(function () {
$(document).on("click", "input.chks", function (e) {
    var _this = $(this);
    var isChecked = _this.is('input:checkbox:checked');
    $.post('@Url.Action("Update","Home")',
           {
               id=this.attr("id") ,
               newValue= isChecked
           }, function (data) {
               // do something with the response
           });

});
</script>