执行HttpPost以在数据库中存储CheckBox选定值

时间:2011-12-24 13:15:27

标签: asp.net-mvc-3 checkbox checkboxlist

我已提及此question以了解如何在数据库中发布checkbox个所选值。

但是,由于selected,我无法在debug上获得NullReferenceException个值(请参阅下面的快照)。

这是我的代码:

Model:

 public class ProductModel
 {
    public string ProductId { get; set; }
    public string ProductName { get; set; }
    public bool Selected { get; set; }
    public string[] CheckedColumn { get; set; }
 }

View:

     @model IEnumerable<DemoApp.Models.ViewModels.ProductModel>

    @{
               ViewBag.Title = "CheckView";
     }

   <table> 
     <tr>
        <th>
          ProductId
        </th>
     <th>
        ProductName
     </th>
     <th>
        Selected
    </th>
        <th></th>
    </tr>

  @foreach (var item in Model) 
 {
   <tr>
      <td>
      @Html.DisplayFor(modelItem => item.ProductId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ProductName)
    </td>
    <td>
        @Html.CheckBoxFor(modelItem => item.Selected)
     </td>
   </tr> 
  }

  </table>
   @using (Html.BeginForm())  
   {
  <div>
  <input type="submit" name="AddResult" value="CheckView"/>
  </div>
    }

Controller:

    [HttpGet]
    public ActionResult CheckView()
    {
        DatabaseEntities db = new DatabaseEntities();

        var prodList = from b in db.SysUser3
                       select new ProductModel
                       {
                           ProductId = b.ProductId,
                           ProductName = b.ProductName,
                           Selected = b.SelectedProducts
                       };
        var p = prodList.ToList();
        return View(p);

    }

    [HttpPost]
    public ActionResult CheckView(FormCollection collection)
    {
        try
        {
            ProductModel model = new ProductModel();
            // Get all the selected checkboxlist, do db insertion
            model.CheckedColumn = collection["CheckView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }

    }

以下是我在运行项目时拍摄的一些快照

After selecting the values from below figure

enter image description here

enter image description here

我在视图或控制器中做错了什么?有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:6)

您的模型为null,因为您没有从Controller ACtion传递任何内容。 CheckView ActionMethod应该是生成视图的操作。然后你的“CheckView”按钮应该调用另一个看起来像你以前的CheckView的HttpPost动作。如果评论有帮助,请告诉我。

针对视图的操作结果方法:

    public ActionResult CheckView(ProductModel model)
    {
       return View("CheckView", model);
    }

点击按钮的操作。

[HttpPost]
    public ActionResult TestView(FormCollection collection)
    {
        try
        {
            ProductModel model = new ProductModel();
            // Get all the selected checkboxlist, do db insertion
            model.CheckedColumn = collection["CheckView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }

    }

在查看类似这样的内容:

@using (@Html.BeginForm("TestView", "Controller Name"))
{
//all your input here

// submit button here
}

答案 1 :(得分:1)

我同意博贝克。问题是您没有在后期操作上重新初始化模型。像这样:

[HttpGet]
    public ActionResult CheckView()
    {
        DatabaseEntities db = new DatabaseEntities();

        var prodList = from b in db.SysUser3
                       select new ProductModel
                       {
                           ProductId = b.ProductId,
                           ProductName = b.ProductName,
                           Selected = b.SelectedProducts
                       };
        var p = prodList.ToList();
        return View(p);

    }

    [HttpPost]
    public ActionResult CheckView(FormCollection collection)
    {
        try
        {
            ProductModel model = new ProductModel();
            // Get all the selected checkboxlist, do db insertion
            model.CheckedColumn = collection["CheckView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            return RedirectToAction("Index");
        }
        catch
        {
            ModelState.addModelError("", "There was an issue processing the results.");
            DatabaseEntities db = new DatabaseEntities();

            var prodList = from b in db.SysUser3
                       select new ProductModel
                       {
                           ProductId = b.ProductId,
                           ProductName = b.ProductName,
                           Selected = b.SelectedProducts
                       };
            return View(prodList.ToList());
        }

    }

您可能还想重构select语句以避免重复代码。当你处理列表时,它们不会维持它们的状态。

答案 2 :(得分:1)

这是一个已知的错误。将javascript函数附加到您的提交按钮,该按钮将遍历复选框选项数组并使用.selected = true;在被检查的那些。只有这样他们才能通过邮件发送。