2

ASP.NET MVC validation shows errors after loading a page (in ValidationSummary), I want to show it after click a "submit" button. My code is:

layout:

<html lang="en">
<head>
    @Styles.Render("~/Content/css")
    @RenderSection("head", required: false)
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @RenderBody()

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

page:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
....
}

@section Head {
    @Styles.Render("~/styles/jquery-ui-bootstrap-custom-theme")
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jqueryui")

bundle file has:

    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-{version}.js"));

    bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.validate*"));

Controller:

        public ActionResult VacancyForm(int? ID, VacancyFormViewModel model)
        {
            VacancyFormViewModel model = new VacancyFormViewModel();
....
            return View(model);
        }
3
  • 1
    You need to show your controller methods Commented Feb 9, 2015 at 19:56
  • added, but what sense in controller? Commented Feb 9, 2015 at 20:24
  • 1
    Is that the GET or POST method? (if its the GET, then that's your problem!) Commented Feb 9, 2015 at 20:25

1 Answer 1

2

You should not have a parameter in a GET method which is a complex object (in your case VacancyFormViewModel). Apart from the ugly query string this creates;

  1. if the model contains large number of properties, or properties with long values, it will exceed the query string limit and throw an exception;
  2. if the model contains properties that are complex objects or collections, then binding will fail (internally a route value dictionary is generated based on the ToString() value of the property)

In your case, the DefaultModelBinder initializes a new instance of VacancyFormViewModel and sets the values of its properties (which may or may not succeed based on the above points). Because you have validation attributes on one or more of the properties, those errors are added to ModelState and therefore displayed in the form.

If the method is for creating a new VacancyFormViewModel,then just initialize a new instance of the model in the method. If its for editing an existing VacancyFormViewModel, pass the ID as a parameter and get the object from the repository based on the ID.

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

Comments

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.