0

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.