views:

29

answers:

1

Conclusion in Images, can be found in the bottom


I'm having some trouble to get how Forms work in MVC (as I'm a WebForms Developer and really wanna start using MVC in one big project)

I took the MVC2 Web Project and add a simple ViewModel to it

namespace TestForms.Models
{
    public class myFormViewModel
    {
        public myFormViewModel() { this.Options = new List<myList>(); }

        public string Name { get; set; }
        public string Email { get; set; }

        public List<myList> Options { get; set; }
    }

    public class myList
    {
        public myList() { this.Value = this.Name = ""; this.Required = false; }

        public string Name { get; set; }          
        public string Value { get; set; }      
        public string Type { get; set; }
        public bool Required { get; set; }
    }
}

Created a Strongly Typed View, passed a new object to the view and run it.

When I press submit, it does not return what's in the Options part... how can I bind that as well?

my view

alt text

filling up the generated form

alt text

when I press Submit the Options part is not passed to the Model! What am I forgetting?

alt text



Conclusion

Changing the View loop to allocate the sequential number, we now have

<%= Html.TextBox("model.Options[" + i + "].Value", option.Value)%>

model is the name of our Model variable that we pass to the View Options is the property name that is of type List and then we use the property name

alt text

alt text

+1  A: 

Looking at your UI it seems that you did not put the data from the Options member on it.

<% foreach (myList obj in Model.Options) { %>
     // Add the object to your UI. they will be serialized when the form is submitted
<% } %>

Also check that you enclose the data in a form element

EDIT:

Sorry! I did'nt realized that you was filling the object inside the controller. Can you please show the code you have in the view?

Lorenzo
I did, I did, or it will never show `Age` and `ZipCode` as it belongs to the object that is passed to the Index `View`... added the image of it
balexandre
Ok! Have a look to this question I made here on SO some time ago. It will give you all the steps to do to make the default model binder serialize the complex object. `http://stackoverflow.com/questions/3800305/building-a-complex-object-step-by-step-where-to-save-it`
Lorenzo
I'm trying but I dont get it, in your example, you do not use Strongly Typed Views, you use the ViewData/Session to keep track of the values. **I tried** having `<%= Html.TextBox("Name", option.Name) %>` but I get the same, my `Options` are not populated in any way :(
balexandre
@balexandre: the key is in using full qualified name for each property. If you look at the answer at some point it says: "Also each field should then have the id of ModelName.Property so that the controller knows where the property is..." followed by a code sample... If you name the property using the fully qualified name the model binder will know where to put that property...
Lorenzo
Also, have a look at this post: `http://stackoverflow.com/questions/616559/default-model-binder-and-complex-types-that-include-a-list`: it uses List as in your case.
Lorenzo
Thank you, that was exactly what I was missing :) the sequential part. Mostly appreciate for all the trouble explain it to me! to bad I can't give +5
balexandre
Not want to abuse your good willing, any answer for this part: http://stackoverflow.com/questions/3883388/dynamic-validation-on-mvc-2 ? It's this example, but now I wanted to use the internal validation for this `ZipCode` and `Age`
balexandre
@balexandre: You are welcome!
Lorenzo