0

The question rather interesting: How can I pass collection to the controller when i have strondly-typed view for create template. This is my ViewModel:

 public class AgencyOfficiesItem
    {    
        public string Address { get; set; }
        public List<PhoneItem> Phones { get; set; }
        public List<SelectListItem> CitiesList { get; set; }
    }

 public class PhoneItem {
        public string Phone { get; set; }
        public string PhoneOperator { get; set; } 
    } 

So, I want to create a view for creating the phone object, which has the phone operator and phone number fields. But, the interesting thing is that i want to have a collection from phone objects and pass them to the controller. Any ideas?

1

1 Answer 1

2

If you just go ahead and do something like this in your view:

@using(Html.BeginForm... // I'm doing this from memory, check the arguments
{
    for( int idx = 0; idx < Model.Phones.Count; idx++ )
    {
        @Html.EditorFor(m => m.Model.Phones[idx].Phone)
        @Html.EditorFor(m => m.Model.Phones[idx].PhoneOperator)
    }
    // similar for cities, other model properties, etc.
}

and declare your post method as follows

[HttpPost]
public ActionResult OnPostBack( <modeltype> arg )
{
    if( ModelState.IsValid )
    {
        // action logic, etc.

then all should be well. MVC will define the resulting textboxes in a way that it will be able to link them back up with the model on postback. I believe what it does is declare the name attribute on the textbox to be, for example, Phones_1_Phone. You can check by looking at the generated HTML in your browser.

BTW, there are other ways of getting this to work, too. You can use keys, with a Dictionary<> rather than a List<>, and sparse arrays, too, I believe. And of course you'll want to do something more than just generate a bunch of textboxes on your page -- some labels would be nice :).

I found that a great tutorial on this and much more is Steven Sanderson's Pro ASP.NET MVC 2 Framework, from Apress. I started off knowing nothing about MVC just a couple of months ago, and now I'm quite comfortable with it (although not yet an expert).

Oops, forgot to mention something important: my example uses MVC3 and its Razor view engine. You'll need to translate it into MVC2 syntax if you're not using MVC3. But you should give MVC3 a look, I find it more intuitive than its predecessors. Not to mention less "wordy".

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

1 Comment

Thanks, but i know about this approach. The problem is that my page isn't for editing, it's for creating. And i don't know exaclty how many textboxes there will be. For instance, I have one field for phone, and if user wants to add one more, he clicks to the button "Add" and one more filed appears.

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.