views:

185

answers:

2

I have this one in View:

 <%
     foreach (var item in (List<MyType>)ViewData["MyTypeArray"])
                {
            %><tr>
                <td>
                    <%=Html.Encode(item.Name)%>
                </td>
                <td>
                    <%=Html.CheckBox("MyTypeFlags" + item.BitNumber),
                    /* Model goes here*/, 
                   new {@value = (1 << item.BitNumber)})%> // html attr
                </td>
            </tr>
            <%        
                }
            %>

and I want do smth like this in Controller:

  foreach (var item in MyDynamicallyCreatedArray)
            {
 //if (["MyTypeFlags" + item.BitNumber] != 0) // This shoud be changed
 }

Question is how should I declare MyDynamicallyCreatedArray and go through the cycle?

+1  A: 

You should look at using a "view model". You basically create "Models" just for your view that contain the data items you need in your view.

I use these quite often and they are really a great way of getting data in and out of your view.

For an example you can view here: http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx

TehOne
I understand that's not very good decision, and I should use strongly typed views, but just now I need as is
Alexander
+1  A: 

Take a look at Phil Haack's post, it gets a bit tricker with checkboxes as if a box is unchecked then it doesnt submit a value.

Model Binding to a List

http://haacked.com/archive/0001/01/01/model-binding-to-a-list.aspx

David Liddle