执行HttpPost在数据库中存储复选框所选值 [英] Performing HttpPost to store CheckBox Selected Values in Database
问题描述
我已经提到这问题,了解如何在数据库中发布复选框
所选值。
由于 NullReferenceException $ c>,因此无法在
debug
上获得我
public class ProductModel
{
public string ProductId {get ;组; }
public string ProductName {get;组; }
public bool Selected {get;组; }
public string [] CheckedColumn {get;组; }
}
@model IEnumerable< DemoApp.Models.ViewModels.ProductModel>
@ {
ViewBag.Title =CheckView;
}
< table>
< tr>
< th>
ProductId
< / th>
< th>
ProductName
< / th>
< th>
已选择
< / th>
< th>< / th>
< / tr>
@foreach(模型中的var项)
{
< 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 =submitname =AddResultvalue =CheckView/>
< / div>
}
控制器:
[HttpGet]
public ActionResult CheckView()
{
DatabaseEntities db = new DatabaseEntities );
var prodList = from b.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();
//获取所有选中的checkboxlist,做db插入
model.CheckedColumn = collection [CheckView]。Split(new char [] {','},StringSplitOptions.RemoveEmptyEntries);
return RedirectToAction(Index);
}
catch
{
return View();
}
}
运行项目时拍摄的快照
我在View或Controller中做错了什么?有人可以帮助我吗?
您的模型为null,因为您不传递任何东西从Controller ACtion。您的CheckView ActionMethod应该是生成视图的操作。然后你的CheckView按钮应该调用另一个HttpPost的动作,看起来像你以前的CheckView。
Action的结果方法:
public ActionResult CheckView(ProductModel model)
{
return View(CheckView,model);
}
单击按钮操作。
[HttpPost]
public ActionResult TestView(FormCollection collection)
{
try
{
ProductModel model = new ProductModel();
//获取所有选中的checkboxlist,做db插入
model.CheckedColumn = collection [CheckView]。Split(new char [] {','},StringSplitOptions.RemoveEmptyEntries);
return RedirectToAction(Index);
}
catch
{
return View();
}
}
@using(@ Html.BeginForm(TestView,Controller Name))
{
//这里的所有输入
//这里提交按钮
}
I have referred this question to learn how to post the checkbox
selected values in the database.
However, I am not able to get my selected
values on debug
because of a NullReferenceException
(see snapshots below) .
Here is my code:
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();
}
}
Here are some of the snapshots I took while running the project
Am I doing something wrong in my View or Controller? can someone please help me ?
Your model is null because you are not passing anything from Controller ACtion. Your CheckView ActionMethod should be an action that generates the view. Then your "CheckView" button should call another HttpPost action that would look like your previous CheckView. Let me know if it helps in the comments.
Action Result method that for the View:
public ActionResult CheckView(ProductModel model)
{
return View("CheckView", model);
}
Action for button click.
[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();
}
}
And in View something like this:
@using (@Html.BeginForm("TestView", "Controller Name"))
{
//all your input here
// submit button here
}
这篇关于执行HttpPost在数据库中存储复选框所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!