I'm still relatively new to ASP.NET MVC 3, and I have a conceptual question for those of you with more experience here.
Lets say for (simple) example I have a signup form for an event. The form would show the name of the event, and a text box where the user could enter their name. The ViewModel would look something like this:
public class SignupFormViewModel {
public string EventName { get; set; }
[Required]
public string VolunteerName { get; set; }
}
The Razor view might look something like this:
<h2>Sign up for @Model.EventName</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<div class="editor-field">
@Html.EditorFor( model => model.VolunteerName )
@Html.ValidationMessageFor( model => model.VolunteerName )
</div>
}
If the user submits the form back to the server and the ModelState is invalid, we return the original View and pass back the Model so we can display the errors. (we're assuming for this example that this is an error not handled on the client)
The trouble is that in my example view shown above, the EventName is not submitted with the form data. I only want to use it for page content, not a form field. But if I have to go back and display validation errors to the user, I've lost the EventName property.
So what are my options for preserving this non-form-field item through a post which comes back to the same view?
I know I can make a hidden field to hold the EventName property, but there's just something that doesn't smell right about being forced to put every non-form-field property into a hidden field. I also know that I could go into the HttpPost action in my controller, and reload that data back into the Model before returning the view, but that feels clunky as well.
I guess I have a good understanding of the basic ways to do this, but I was wondering if there was a better way or best practice that felt cleaner... or if I just have to learn to deal with it :-)
Thanks