1

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?

5
  • 1
    Why aren't you just returning model to the view? Do you have to return the URL as well? IE. have you tried return(View model); and it doesn't work? Commented May 6, 2013 at 6:01
  • 1
    The view where the form information is entered is home/index but the controller action and result view is EnrolResult. Simply putting return View(model) will be displaying EnrolResult instead of pointing them back to the view where they should correctly enter information into the form. Commented May 6, 2013 at 6:06
  • Are you including the validation javascript on your view page? Commented May 6, 2013 at 6:21
  • The view that post to EnrolResult is the view you are showing us in your question? I hope not because it's clearly showing you are posting to home.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. Commented May 6, 2013 at 6:45
  • You can try Html.ValidationSummary(false) if set to true. Commented Dec 27, 2013 at 9:36

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

updated my answer to show how to enable ValidationSummary() with property-level error messages.
Turns out the solution was a fairly basic one, in return View("~/Views/Home/Index", model); it needed to be index.cshtml instead of simply index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.