mvc中的复选框,显示每个客户的复选框 [英] checkbox in mvc, display a checkbox for each customer
问题描述
mvc中的复选框有问题。新编码,但我创建了一个数据库第一个MVC4项目。在我的一个视图中,我想为每个客户显示一个复选框,当选中并按提交时,数据库表应该更新为true,意味着产品已分派。如果有人可以看到解决方案或有一个很好的教程复选框,请告诉我。提前致谢!
查看:
Having trouble with checkbox in mvc. New to coding, but I have created a database first MVC4 project. In one of my views I want to display a checkbox for each customer, when checked and submit pressed the database table should update to true meaning product dispatched. If anyone can see the solution or has a good tutorial on checkbox please let me know. Thanks in advance!
View:
@model IEnumerable<sales1.models.userprofile>
<input type="checkbox" name="RememberMe" id="RememberMe" value="true" />
@using (Html.BeginForm())
{
<input type="submit" value="Save"/>}
控制器:
Controller:
[HttpGet]
public ViewResult NewOrder()//
{
var userprofiles = db.UserProfiles;
return View(userprofiles);
}
public ActionResult NewOrder(bool rememberMe)
{
bool remember = Convert.ToBoolean(rememberMe);
db.SaveChanges();
return View(remember);
}
当我运行上面的内容时我得到错误:传入字典的模型项是'System'类型.Boolean',但是这个字典需要一个'System.Collections.Generic.IEnumerable`1
When I run the above I get the error:The model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1
推荐答案
类型的模型项目如果我理解你的问题,你正在通过来自控制器的对象userprofiles,但它是sholud是列表类型,因为在视图中你收到它IEnumerable类型
所以你可能修改的代码如下:
if i am understanding your question right you are passing a object userprofiles from controller but it sholud be list type because in view you are receiving it IEnumerable type
so your possibly modified code is below :
View: @model IEnumerable
@foreach(var item in Model)//add foreach loop for iteration
[
<input type="checkbox" name="RememberMe" id="RememberMe+'@item'+ " value="true" />
}
@using (Html.BeginForm())
{
<input type="submit" value="Save" />}
Controller:
[HttpGet]
public ViewResult NewOrder()//
{
var userprofiles = db.UserProfiles;
return View(userprofiles.TOList());//return a list not a single object
}
public ActionResult NewOrder(bool rememberMe)
{
bool remember = Convert.ToBoolean(rememberMe);
db.SaveChanges();
return View(remember);
}
我希望这将有助于
I hope this will help
这篇关于mvc中的复选框,显示每个客户的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!