Reputation: 816
I'm using ASP.NET MVC 4.
I don't think my ModelState is being passed properly,
Controller action: Home/EnrolResult as follows:
[HttpPost]
public ActionResult EnrolResult(UploadModel model)
{
if (ModelState.IsValid)
{
// my codes in here
return View();
}
return View("~/Views/Home/Index", model);
}
View: Home/Index, which is as follow:
@using (Html.BeginForm("EnrolResult", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary()
@Html.LabelFor(m => m.Firstname)
@Html.TextBoxFor(m => m.Firstname)
@Html.LabelFor(m => m.Surname)
@Html.TextBoxFor(m => m.Surname)
<li data-role="fieldcontain">
<input type="submit" value="Enrol" />
</li>
}
When I enter the details correctly it functions correctly and displays the enrolresult view.
When I enter the details incorrectly it returns to Home/Index but doesn't display the validation error of which form wasn't entered.
The model is set up correctly as I have tested it where I changed the EnrolResult action to an index action and the validation error displayed correctly, where I used view(model) meaning the ModelState wouldn't have to be passed like I'm trying to do in this case.
Is the way I'm doing it correct? I've seen people use TempData but I thought this method was meant to work?
Upvotes: 1
Views: 10117
Reputation: 926
Validation message helpers are missing:
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary()
@Html.LabelFor(m => m.Firstname)
@Html.TextBoxFor(m => m.Firstname)
@Html.ValidationMessageFor(m => m.Firstname)
@Html.LabelFor(m => m.Surname)
@Html.TextBoxFor(m => m.Surname)
@Html.ValidationMessageFor(m => m.Surname)
<li data-role="fieldcontain">
<input type="submit" value="Enrol" />
</li>
}
edit:
If you don't want to use ValidationMessageFor
I believe you need to use a different overload of Html.ValidationSummary(false). If I remember correctly the default behavior of ValidationSummary hides the property-level errors to avoid duplication.
Upvotes: 3