views:

247

answers:

4

I'm creating a checkbox list to handle some preferences as follows...

        <ul>
            <%foreach (var item in ViewData["preferences"] as IEnumerable<MvcA.webservice.SearchablePreference>)
              {
                  var feature = new StringBuilder();
                  feature.Append("<li>");
                  feature.Append("<label><input id=\"" + item.ElementId + "\" name=\"fpreferences\" type=\"checkbox\" />" + item.ElementDesc + "</label>");
                  feature.Append("</li>");
                  Response.Write(feature);
              }
            %>
        </ul>

The data handed to the viewdata of SearchablePreference[] and the list displays fine.

The question is; How would I repopulate the selected boxes if the page had to return itself back (i.e failed validation).

In webforms its handled automatically by the viewstate; with the other input elements I'm simply passing the sent-data back to the page via ViewData.

Thanks

+2  A: 

Use Html.Checkbox instead.

RichardOD
Good point; have to admit I was faffing
Chris M
Found this useful: Gotcha 2 -- http://www.mikesdotnetting.com/Article/109/ASP.NET-MVC-Entity-Framework-One-to-Many-and-Many-to-Many-INSERTS
Chris M
A: 

You have to read off all of the values, then add them to the ViewData of the view you come back to, writing them into the checkboxes in the view display (preferably with Html.Checkbox). To my knowledge, Html.Checkbox does not automatically manage viewstate for you.

Assuming i have a standard CRUD object I very often have something like this in the Edit view. (I use the same view for edit and new)

<% var myObj = ViewData["theObj"] as MyObj; %>

<input (other input attribute here) value="<%=(myObj == null ? "" : myObj.AppropriateProperty)%>" />
Russell Steen
Correct me if I am wrong, but MVC does not use ViewState, and ViewState should NOT be used in MVC. If you need ViewState use Web Forms.
Martin
The original thing says that in Webforms you dont need to do this as you have Viewstate; I'm not planning on rewriting a whole app for one inane bit of functionality. Hence looking for the alternative. Ta
Chris M
@Russell. I think you are confusing ViewModel with ViewState. The HtmlHelpers firstly look at ViewData.ModelState["controlName"].Value.RawValue then ViewData.Eval["controlName"] when repopulating. You can read all about this in the book Pro ASP.NET MVC Framework which I highly recommend.
RichardOD
Well, the other posts seem to indicate that Html.Checkbox will cache the viewstate for you. If that is the case, then great! I've just never had it work for me. I'm probably doing something odd in my two projects that is preventing it from working.
Russell Steen
@RichardOD - Thanks for the tip, I'll read some more.
Russell Steen
@RichardOD - Looking over some MVC documentation, it is clear that it doesn't use ViewState. My question is where the data goes in the middle. If I have a input that is called "joe", does MVC automatically drop that value into ViewData["joe"] for me, or do I have to do it in my controller during processing - ViewData["joe"] = formCollection["joe"];
Russell Steen
A: 

I agree with RichardOD, use Html.Checkbox

but if you insist on doing it manually, here´s how

Use an object to hold the data (whether something is selected or not) and give that to the view, when populating the checkboxes, check if the relevant value is set and if so set html "checked=\"checked\"" on the checkbox

The first time you render this, the object will have only false datas, so nothing will be selected.

AndreasKnudsen
A: 

You have way too much code in the View. This should be done in the controller. The View should be:

    <ul>
        <%foreach (var item in ViewData["preferences"] as IEnumerable<MvcA.webservice.SearchablePreference>)
          {
              <li>
                  <%=Html.Checkbox(........) %>
              </li>
          }
        %>
    </ul>
Martin