1

I have a form, with a partial view inside it to render several child controls.

Main view :

@model Test_mvc.Models.Entity.Question
@{
    ViewBag.Title = "Edit";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
    @*snip*@
    <fieldset>
        <legend>Profils</legend>
        @Html.Action("CheckboxList", "Profil", new { id_question = Model.id })
    </fieldset>
    <p>
        <input type="submit" value="Enregistrer" />
    </p>
}

Profil controller (for the partial view) :

    [ChildActionOnly]
    public ActionResult CheckboxList(int id_question)
    {
        var profils = db.Profil.Include("Profil_Question")
            .OrderBy(p => p.nom).ToList();
        ViewBag.id_question = id_question;
        return PartialView(profils);
    }

Profil.CheckBoxList view :

@model List<Test_mvc.Models.Entity.Profil>
@foreach (var p in Model)
{
    <input type="checkbox" name="profil_@(p.id)"
        @if (p.Profil_Question.Where(pc => pc.id_question == ViewBag.id_question).Any())
        {
            @:checked="checked"
        } />
    @Html.Label("profil_" + p.id, p.nom)
    <br />
}

(I don't want to use @Html.CheckBox because I don't like being sent "true,false" when the checkbox is checked).

Today, if I want to get the checkboxes that have been checked, I do this, but I think it's awful :

Question controller (for the main view) :

    [HttpPost]
    public ActionResult Edit(Question question)
    {
        if (ModelState.IsValid)
        {
            db.Question.Attach(question);
            db.ObjectStateManager.ChangeObjectState(question, EntityState.Modified);
            db.SaveChanges();

            // this is what I want to change :
            foreach (string r in Request.Form)
            {
                if (r.StartsWith("profil_") && (Request.Form[r] == "true" || Request.Form[r] == "on")) {
                    var p_q = new Models.Entity.Profil_Question();
                    p_q.id_profil = int.Parse(r.Replace("profil_", ""));
                    p_q.id_question = question.id;
                    db.AddToProfil_Question(p_q);
                }
            }

            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(question);
    }

How would you replace the "foreach" in the last code section ?

Thanks

thomasb
  • 5,816
  • 10
  • 57
  • 92

1 Answers1

1

The first thing I would try would be to give all my checkboxes the same name and put the @id as the value of the box:

@foreach (var p in Model) {     
     <input type="checkbox" name="profil_checkbox" value="@p.id" 
     @if (p.Profil_Question.Where(pc => pc.id_question == ViewBag.id_question).Any()) 
     {
         @:checked="checked"
     } />  
@Html.Label("profil_" + p.id, p.nom)     <br /> } 

Then rather than searching for profil_@id I should get an array of results for profile_checkbox which is much easier to work with. I don't recall exactly how MVC3 processes this, so I can't guarantee what exactly you'll get in the postback, but that should be easy enough to check during debugging.

glenatron
  • 11,018
  • 13
  • 64
  • 112
  • OK, it's a bit better, but I still have to use Request.Form, don't I ? How do I use a ViewModel to "bind" the partial view's values to the controller ? – thomasb May 20 '12 at 11:13
  • 1
    You would need to add that list as an attribute of your viewmodel, both the list of possible values and the list of selected values, then take a look at the second answer here: http://stackoverflow.com/questions/4872192/checkboxlist-in-mvc3-0 – glenatron May 21 '12 at 08:25