Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 执行HttpPost以在数据库中存储选中的复选框值_Asp.net Mvc 3_Checkbox_Checkboxlist - Fatal编程技术网

Asp.net mvc 3 执行HttpPost以在数据库中存储选中的复选框值

Asp.net mvc 3 执行HttpPost以在数据库中存储选中的复选框值,asp.net-mvc-3,checkbox,checkboxlist,Asp.net Mvc 3,Checkbox,Checkboxlist,我引用此内容是为了了解如何在数据库中发布复选框所选值 但是,由于出现NullReferenceException(请参阅下面的快照),我无法在debug上获取所选的值 这是我的密码: 型号: public class ProductModel { public string ProductId { get; set; } public string ProductName { get; set; } public bool Selected { get; set; }

我引用此内容是为了了解如何在数据库中发布
复选框
所选值

但是,由于出现
NullReferenceException
(请参阅下面的快照),我无法在
debug
上获取所选的

这是我的密码:

型号:

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

     @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>
    }
以下是我在运行项目时拍摄的一些快照


我的观点或控制者是否做错了什么?有人能帮我吗?

您的模型为空,因为您没有从控制器操作传递任何内容。CheckView ActionMethod应该是生成视图的操作。然后,您的“CheckView”按钮应该调用另一个HttpPost操作,该操作看起来与您以前的CheckView类似。如果在评论中有帮助,请告诉我

用于视图的操作结果方法:

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

    }

您的模型为空,因为您没有从控制器操作传递任何内容。CheckView ActionMethod应该是生成视图的操作。然后,您的“CheckView”按钮应该调用另一个HttpPost操作,该操作看起来与您以前的CheckView类似。如果在评论中有帮助,请告诉我

用于视图的操作结果方法:

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

    }

我同意博贝克的观点。问题是您没有在post操作中重新初始化模型。大概是这样的:

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

// submit button here
}
[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语句以避免重复代码。当你处理列表时,它们不会保持它们的状态。

我同意bobek的观点。问题是您没有在post操作中重新初始化模型。大概是这样的:

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

// submit button here
}
[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语句以避免重复代码。当您处理列表时,它们不会保持其状态。

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

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

您提到的第一段代码,是HttpGet方法吗?请检查问题中我更新的代码。我也加入了HttpGet方法,我也尝试过。虽然在你提到的第二个代码块中,我遇到了一些问题。我对你加入的新“TestView”代码感到困惑。请你帮我一下好吗?是的。因此,基本上,表单正在调用TestView。每当您单击表单上的submit按钮时,TestView操作就是要响应的操作。CheckView操作是在开始时加载视图的操作。请看这里:您提到的第一块代码,是HttpGet方法吗?请检查问题中我更新的代码。我也加入了HttpGet方法,我也尝试过。虽然在你提到的第二个代码块中,我遇到了一些问题。我对你加入的新“TestView”代码感到困惑。请你帮我一下好吗?是的。因此,基本上,表单正在调用TestView。每当您单击表单上的submit按钮时,TestView操作就是要响应的操作。CheckView操作是在开始时加载视图的操作。请过来: