EDIT AFTER:
I should have worded my original question better: In the example below, I am using two forms:
using (Html.BeginForm....
In the controller, how would I validate only one of the forms, and NOT the entire model? Is this even possible? Or, am I trying to use MVC in a way it's not intended? I've been an ASP.NET forms guy for many years. Still learning MVC.
// END EDIT
I have a single view with a form that I need to present as a two-part (or two-page) form. Both parts have some required fields. I am able to simulate the multi-page form alright, but what I'm having trouble with is the validation. With each post, it's validating all of the fields on the entire view. How would I get it to validate only the fields that are currently visible?
Here's what I have now (simplified):
Model:
public Boolean Page1Complete { get; set; }
public Boolean Page2Complete { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page1Question1Required")]
public int? LikelyToReturn { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page2Question1Required")]
public int? RecomendToFriend { get; set; }
View:
if (!Model.Page1Complete)
{
using (Html.BeginForm("PatientSatisfactionSurveyPage1", "Forms", FormMethod.Post, new { id = "patient-satisfaction-survey-page-1", @class = "full-form" }))
{
@for (var a = 0; a < 11; a++)
{
@a - @Html.RadioButtonFor(model => Model.LikelyToReturn, @a)
}
<input type="submit" id="page1-submit" name="page1-submit" value="Continue" class="btn green2">
}
}
else
// Page1 was submitted successfully. Display Page 2
{
using (Html.BeginForm("PatientSatisfactionSurveyPage2", "Forms", FormMethod.Post, new { id = "patient-satisfaction-survey-page-2", @class = "full-form" }))
{
@for (var a = 0; a < 11; a++)
{
@a - @Html.RadioButtonFor(model => Model.RecomendToFriend, @a)
}
<input type="submit" id="page2-submit" name="page2-submit" value="Complete" class="btn green2">
}
}
Controller:
[HttpPost]
public ActionResult PatientSatisfactionSurvey([Bind]PatientSatisfactionSurveyPage pss)
{
//Process and validate the first page
if (Request.Form["page1-submit"] != null)
{
if (ModelState.IsValid)
{
pss.Page1Complete = true;
// Page 1 Logic...
}
}
//Process and validate the first page
if (Request.Form["page2-submit"] != null)
{
if (ModelState.IsValid)
{
pss.Page2Complete = true;
// Page 2 Logic...
}
}
}