5

Following this post and the other posts that it links to, I have successfully built a page with a variable legnth editable list.

Now, I need to do another page with similar functionality, but going a step further. Basically I need to create a variable length list of objects as I have already done, but in this case each object in the list needs to have its own variable length editable sub list.

I can build the view with no problem but I guess I am lost as to how I will handle this in my POST controller method to save the objects.

Will my controller take 2 IEnumerable parameters? I.E.

public ActionResult SaveList(IEnumerable<MainList>, IEnumerable<Sublist>)
{

}

It seems like this would work but I have no way of associatiing the objects in the sublist collection with the appropriate object in the main list collection.

What am I missing?

6
  • If I understand the question correctly, your sublists should be members of your MainList elements. In your view you will have a nested foreach loop to work with the Sublist elements. In the controller you just accept the MainList as a parameter (it should already have the sublists as members). Have you tried this approach? Is there something that doesn't work? Commented Mar 7, 2011 at 12:38
  • I need to give that approach another try, I had tried it in the past without succes, but I may have missed something. You are correct, the sublists are members of the mainlist objects. If I can get this to work, it would makes this pretty simple Commented Mar 7, 2011 at 12:43
  • @Yakimych , unfortunately this method is not working for me...the Sublist collection is empty Commented Mar 7, 2011 at 23:14
  • @stephen776 - can you take a look at the source html of your page and check the name attribute for your list elements and your sublist elements. That could give a clue why the ModelBinder is not picking them up. My guess is you are using partials and passing models into those partials, so the generated names include only the properties inside the sublist item, while it should contain the property of the main list first. Commented Mar 8, 2011 at 20:38
  • @Yakimych that worked. Had to make sure the name property assigned to the list elements matched up properly. Post it as an answer and the checkmark is yours. Thank you. Commented Mar 9, 2011 at 0:04

2 Answers 2

2

Summing up the results of our comment-discussions as an answer:

The sublists should be members of your MainList elements. In the View you will have a nested foreach loop to work with the Sublist elements. In the controller you just accept the MainList as a parameter (it should already have the sublists as members). In order for the ModelBinder to pick up the sublists correctly, the names of the inputs should include the names of main list first (and not just the properties inside the sublist item).

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

Comments

1

I think you need to build a ModelBinder class to accept these parameters the way you want into your controller object.

1 Comment

I will have to look into this some more

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.