views:

323

answers:

2

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.

+1  A: 

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

RandomNoob
Thanks for your answer and it might be helpful to do this. I don't think this will allow me to have one list within the ScorecardFormViewModel which I could update without having lots of redundant properties on the ViewModel such as "private int Hole1Par" which reference the list. If it can, could you explain how I can do this as this would be perfect. Thanks
bobwah
+1 for putting the effort in :) I am going for something similar here, however in a situation where say i'd have 19 holes but didn't know in advance (ok this won't happen with Golf but just for future projects) how can I make the ViewModel so that it displays these and you can update them?
bobwah
A: 

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).

TimGThomas