2

What is the cleanest, most ASP.NET MVC3 way, for lack of a better term, to model a collection of finite count and display the web form using Razor?

I understand I can throw a List in the model and loop through it in the view, but that would not in any way automagically tell EditorFor to render a finite number of text boxes or whatever it is I want.

Is there any more automatic way to build a view model that works in conjunction with the Razor view to tell it exactly how many items I want in my collection and thus it could automatically render the right number of form elements?

2
  • What don't you like about the foreach syntax? Commented Jan 9, 2012 at 16:25
  • I like it fine, and if there is no better way, that's exactly what I will use. My question is, using EditorFor, is there any more automatic way to wire up a model with a property that's a fixed length array or collection of finite amount such that the view would automatically display the right number of form elements. So in my case I need to collect exactly five salaries via exactly five text boxes... Commented Jan 9, 2012 at 16:27

3 Answers 3

1

If we are talking about small numbers of simple data types, then in truth (although not the answer most want to hear) is that you should have a single field in the model for each instance of the item. So for 5 salaries, you would have salary1, salary2, etc. in your model.

If you want to abstract this a bit - as well as make it more resistant to change/ handle larger numbers, etc. you could attach some custom metadata to the field indicating the number of values you want. You would then need either a helper or a custom template (or combination) to take the metadata and render the correct number of input fields (with the correct names, id's, etc.).

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

1 Comment

That's what I did initially, salary1, salary2, etc. But it just feels clunky doesn't it? Each one needs the exact same validation logic and further more I need them validated as a collection of values, aka, you must enter all five. With all of the other attempts to automate model binding to a view, I am surprised this scenario is overlooked...
1

Using a list as the model for your view is the recommended way of doing this. It's simple, clean and efficient.

You could create EditorFor templates that render lists but they would essentially be the same thing as the view with some added overhead due to the templating system.

2 Comments

In your view, what is the best way to limit the List count to a finite number of items?
I'm not sure I understand your problem. You want 10 elements out of the 34 that get passed from the controller or something like that?
1

I really don't understand what your problem is. Why would you need to limit it? You just put however many records in the list, and that's how many it will render. If you only need 5 records in a list, do you go out of your way to make sure the list can only contain 5 elements? No. The same holds for your view.

1 Comment

The question is, is there any more automated way to model and wire this up. I'm not going to tell the people that write the specs for my apps that there's no reason to limit the number to exactly five. There is a very good reason. You have no clue what the purpose of this web form is and you don't need to know. If the answer is that there is no more automated way, that's fine, say that and I will accept it.

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.