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?
EnrolResult
is the view you are showing us in your question? I hope not because it's clearly showing you are posting tohome.Index
. That's the reason why you are not getting validation messages, because home.index view is a different view than the one you used for posting. – von v. May 6 at 6:45