1

I've got the next question about the right way of adding items to a list in MVC.

Let's say I got the following model:

public class Student
{
    public String Name { get; set; }
    public List<Lesson> Lessons { get; set; }
}

Now I'm in the Student Create view in which the user can add multiple Lessons. The way the view looks is that there is a dropdown in which you can select a lesson and a button to add a new dropdown. This way a person can add multiple lessons to the Lessons variable.

Now I tried alot of different approaches but never seemed to have the right one because I don't like having this in the view:

 @Html.DropDownListFor(model => model.Lessons[0].Id, new SelectList(...), "Select lesson")

And change the 0 to 1..2 etc with jquery or what so ever.

What are your approaches on those views in which you dynamicly add multiple items?

1 Answer 1

2

You should look at defining an editor template:

If you write your template correctly, asp.net will render controls with the names similar to:

Lesson[0]_Title

UPDATE: Here's how:

Create a folder in views\shared called EditorTemplates Then create a partial view with a model of type Lesson inside here. Make sure you call this partial view "Lesson.cshtml" (or ascx if you're not using razor)

Then you can do...

@Html.EditorFor(model => model.Lessons) 

...and asp.net will magically figure out that because Lessons is an IEnumerable of lesson, it should loop and render an editor for each one. Pretty neat!

Some links:

http://forums.asp.net/t/1846290.aspx/1?Editor+template+of+type+IEnumerable+SelectListItem+with+MVC+area

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

10 Comments

That would be a nice solution. Though we need the right template then indeed. It would be awesome if you could find the link you're talking about!
I'm struggling actually... I'm going to see if I can knock up an example quickly myself...
Thanks for pointing me at the EditorTemplates. Though if a new Student is made we don't know how many Lessons he will follow. So we should be able to dynamicly add new Editors for the Lessons.
Yes sorry... I have provided an update that is not strictly what you asked for, but should be a simpler way of achieveing it. Say you wanted to add x number of lessons, you can then prepopulate that number of lessons in the model, and it will provide an editor for each one which you can save in a single form. It's a little confusing so feel free to ask if you have more questions.
Alternatively there is a very nice solution that can be achieved using jquery and knockout to dynamically create\remove new lessons and then still have it compatible with .net when you submit the form, that I will be blogging about this weekend.
|

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.