ASP.NET MVC List population in strongly typed views

1.2k Views Asked by At

A bit of background:

I'm building an MVC app to store golf course data and have created a Create view page for the courses. This contains a partial view of a scorecard that I am going to use for other things such as recording results etc. I've currently built the scorecard so it fires off jQuery triggers when it is edited. From which the course create has jQuery code bound to these events and populates hidden form inputs for each of the 18 holes.

Question:

I was wondering if I need to have a mass of hidden form inputs on my create page to store the fired values or if I can have a list in my view model that I can update somehow.

Any more elegant solutions than what I have at the moment would be helpful.

2

There are 2 best solutions below

1
On BEST ANSWER

You could create a custom class like ScorecardFormViewModel and include the items you need as properties and have your View inherit ScorecardFormViewModel

EDIT:

public class ScoreCardFormViewModel {

// Properties
public List<ParValues> { get; private set; }

public GolfCourse GolfCourse {get; private set;}

public ScoreCardFormViewModel(int golfCourseId)
{
    GolfCourse = SomeMethodToGetGolfCourseFromModel(golfCourseId);
    // Some way to populate ParValues
}

}

I just created a list of ParValues, maybe an over simplification, but you could put as many properties as you wanted here

0
On

I believe this post of Phil Haack's might be useful to you. It describes how to use the default ASP.NET MVC model binder to populate a list with data from multiple client-side input elements (in your example, it would be the various text boxes, etc. for each Hole).