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

Par*_*shi 1 checkbox checkboxlist asp.net-mvc-3

我已经提到了这个问题,以了解如何checkbox在数据库中发布所选的值.

但是,由于a (参见下面的快照),我无法获得我的selected值.debugNullReferenceException

这是我的代码:

Model:

 public class ProductModel
 {
    public string ProductId { get; set; }
    public string ProductName { get; set; }
    public bool Selected { get; set; }
    public string[] CheckedColumn { get; set; }
 }
Run Code Online (Sandbox Code Playgroud)

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>
    }
Run Code Online (Sandbox Code Playgroud)

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

    }
Run Code Online (Sandbox Code Playgroud)

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

选择下图中的值后

在此输入图像描述

在此输入图像描述

我在View或Controller中做错了什么?有人可以帮帮我吗 ?

bob*_*bek 6

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

适用于View的Action Result方法:

    public ActionResult CheckView(ProductModel model)
    {
       return View("CheckView", model);
    }
Run Code Online (Sandbox Code Playgroud)

单击按钮的操作.

[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();
        }

    }
Run Code Online (Sandbox Code Playgroud)

在查看类似这样的内容:

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

// submit button here
}
Run Code Online (Sandbox Code Playgroud)