0

For each each item available i render a partial view, in the partial view I have a checkbox. The purpose is so that the the user can select multiple items at once, then when they hit delete all the items selected will be deleted. Checkbox in my partial view is not within a form currently its just like:

<%= Html.CheckBox("IsSelected", Model.IsSelected)%>

So it displays correctly, but does nothing.

I asked a question previously about binding the value from a model to a checkbox and that worked fine. I did:

 <%= Html.CheckBox("["+itemx+"].IsSelected", x.IsSelected) %>

But now i have create the partial view i was wondering how i would tie this all together and get the values from the partial view?

Thanks.

TBD
  • 771
  • 1
  • 11
  • 27

2 Answers2

2

@Felipe: your answer is not wrong, but a small performance improvement is to not call Html.Partial in the loop.

Instead, call the Html.Partial ones and pass in the IEnumerable as a model and loop through the items in that partial view.

More info on this topic: http://channel9.msdn.com/Series/mvcConf/mvcConf-2-Steven-Smith-Improving-ASPNET-MVC-Application-Performance

J. Salman
  • 61
  • 3
1

It seems that you are using ASPX View engine and my examples are using Razor, but the concept is the same, just adjust the sintaxe for you.

Put your form around the the rendering of your partial view

<form action="@Url.Action("ProcessForm")" method="post">
    <ul>
        @Html.Partial("Checkboxes", Model)
    </ul>

    <input type="submit" value="Send selected items"/>
</form>

Partial View (~/Views/Home/Checkboxes.cshtml)

@model IEnumerable<SandboxMvcApplication.Models.Item>

@{
    ViewBag.Title = "Checkboxes";
}

@foreach (var item in Model)
{
   <li><input type="checkbox" name="items" value="@item.Id" />@item.Title</li>
}

I have answered your previous question (Asp.net MVC get fully qualified URL to an action method about how to send multiple items to an Action and I just changed a little bit the checkboxes rendering for this question.

Community
  • 1
  • 1
seixasfelipe
  • 161
  • 1
  • 7