32

Apologies if the title is unclear.

I'm trying to return my model from a form submit in ASP.NET MVC.

My question is nearly the same as this question, only differing in that I don't have a List<Model> but a model like:

public Model
{
     string UserName {get; set;}
     string Password {get; set;}
     List<Roles> UserRoles {get; set;}
}

where I need the UserRoles as checkboxes that the admin can select from when creating a new user. My question is, I'm unsure how to use a '@Html.CheckBoxFor' against a list. I tried this:

 @for (var i = 0; i < Model.UserRoles.Count();i++ )
 {
   @Html.HiddenFor(model => model.UserRoles[i].RoleID)
   @Html.CheckBoxFor(model => model.UserRoles[i].Selected)
   @Html.LabelFor(model => model.UserRoles[i].Name)
 }

which in no way worked - every label on the page is "Name", and my List was empty in the POST. Can anyone offer me any guidance on this?

Community
  • 1
  • 1
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • 3
    This MVC tutorial has a good example of using checkboxes in the view from your model. Start halfway down the page. http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application – Daniel Simpkins Dec 19 '13 at 16:53

4 Answers4

55

No need to go away from Razor at all.

This works for me:

for (var i = 0; i < Model.UserRoles.Count(); i++)
{
    var role = Model.UserRoles[i];
    @Html.HiddenFor(model => model.UserRoles[i].RoleId)
    @Html.CheckBoxFor(model => model.UserRoles[i].Selected)
    @Html.LabelFor(model=> model.UserRoles[i].Name, role.Name)
}
LiverpoolsNumber9
  • 2,384
  • 3
  • 22
  • 34
10

See below code, this way you don't need to hide the role Id, also when you save the selected roles for the user, you don't need to loop through all roles to see which role is selected.

View

@foreach (Roles info in Model.UserRoles)
{
    <span>
        <input type="checkbox" class="checkbox" name="selectedRoles" value="@info.RoleName" id="@infoRoleName" />
        <label for="@info.RoleName">@info.RoleName</label>
    </span>
}

Action

[HttpPost]
public ActionResult CreateUsers(Model model, string[] selectedRoles)
{
       //
}
Lin
  • 15,078
  • 4
  • 47
  • 49
  • 1
    You don't need to use native HTML. And how does a controller bind 2 models?! – LiverpoolsNumber9 Dec 19 '13 at 17:24
  • 3
    I've used native HTML. But you literally *can't* bind two models. You can't. You just can't. You can't. (Did I mention that you can't). If you could, it would be called "MsVC". – LiverpoolsNumber9 Dec 19 '13 at 17:28
  • 1
    hi @LiverpoolsNumber9, Have you ever see two submit buttons on MVC view, if you know how to solve that problem. you'll know how does this work. – Lin Dec 19 '13 at 17:34
  • 2
    Yeah, if you have **two forms**, you have **two actions**. Not two models. Child actions? Partials? – LiverpoolsNumber9 Dec 19 '13 at 17:36
1

From your code in the view, the post should work fine providing your post action looks like this:

[HttpPost]
public ActionResult Action(Model model)
{
    return View(model);
}

i.e. passing your model across as the argument.

Also make sure you have your model reference in the view too:

@model YourNameSpace.Model
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
0

Post a list of check boxes to server and get list of checked items
linq left join to check whether checked, generating checkboxes,received checked list

View

    List<eDurar.Models.tbl_ISOCetificate> ModList = db.tbl_ISOCetificate.ToList();

    var li = (from cert in db.tbl_ISOCetificate join comCert in db.tbl_CompCertificate on cert.Cert_id equals comCert.CompCer_id into jo from b in jo.DefaultIfEmpty()
              select new {cert.Cert_id,cert.Cert_Name,chkd = b.CompCer_SerId==null?"":"checked"}).ToList();


    foreach (var item in li)
    {       
        @:<div style="width: 30%;  display: inline-block; margin: 1em">
        @:<input type="checkbox" @item.chkd name="CheckedCertificates" value="@item.Cert_id">
        @:<label>@item.Cert_Name</label>
        @:</div> 
    }

Controller

  [HttpPost]
    public ActionResult ManageSurveyGroup(int[] CheckedCertificates)
    {
        return View();
    }