Click here to Skip to main content
15,313,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
HTML
@model IEnumerable<sales1.models.userprofile>

       <input type="checkbox" name="RememberMe" id="RememberMe" value="true" />


           @using (Html.BeginForm())
         {
      <input type="submit" value="Save"/>}

Controller:
C#
[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);

  }

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
Posted
Updated 6-Oct-13 23:15pm
v4

1 solution

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 :

C#
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
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900