0

I have a view with two forms each containing their own

@Html.ValidationSummary()

When the page is loaded, a querystring parameter is checked and if it exists I call:

ModelState.AddModelError("", "Querystring error");

However, this results in the error message appearing in both @Html.ValidationSummary() even if I specify a property in the form model.

I have a work around which is to have a seperate error message property in the model for the form and populate that and then display it if it exists in a label, but wondered if it is possible to specify one individual @Html.ValidationSummary() within a form to allow me to use ModelState.AddModelError?

6
  • 2
    ValidationSummary is per model, not per an HTML form. If you effectively have two models on the page with two validation summaries, look into using partial views. Commented Oct 10, 2015 at 18:20
  • for better understanding can u please explain ur scenario in a dotnetfiddle.net Commented Oct 10, 2015 at 18:23
  • The view has one model which is split to contain two classes which each contain the properties for each form. Maybe partial views is the way to go in this scenario, I'll take a look, thanks. Commented Oct 10, 2015 at 18:36
  • Ok Good, still for my understanding i just created a fiddle .. dotnetfiddle.net/GoMMhy , is this what u are expecting ? Commented Oct 10, 2015 at 19:06
  • @GSerg How would I ensure a call to ModelState.AddModelError is only affecting one of the models when using partial views? When the page loads it checks the query string, calls ModelState.AddModelError if it is not correct, then returns the View passing in the model which contains 2 classes i.e. the models for each form. I'm not sure how using a Partial View would resolve my problem at the moment. Commented Oct 11, 2015 at 11:31

2 Answers 2

1

Following the helpful information I was given by @GSerg, I thought I'd share my resolution.

So instead of having two forms within the same view, I split each form into two separate partial views and called each from within the main view...

@Html.Action("_RequestPartial")
@Html.Action("_LoginPartial")

Each partial view would contain the model to pass to it and a Html.BeginForm with a Html.ValidationSummary() inside.

Then within the Controller, set up the code to return the view as normal (in this case for the Index view)...

[HttpGet]
public ActionResult Index()
{
     return View();
}

Then for the partial views, set up a PartialViewResult for each partial view annotated with ChildActionOnly...

[ChildActionOnly]
public PartialViewResult _RequestPartial()
{
      ... code that populates model you want to pass ...

      return PartialView(model);
}

[ChildActionOnly]
public PartialViewResult _LoginPartial()
{
      ... code that populates model you want to pass ...

      return PartialView(model);
}

Hope this helps someone.

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

Comments

0

To show the specific validation message, please see the snippet code below. Controller :

[HttpGet]
        public ActionResult Index()
        {
            ModelState.AddModelError("Error1", "Querystring error");
            return View(new SampleViewModel());
        }

View :

@Html.ValidationMessage("Error1")

Just tried to create a fiddle to get a complete picture. https://dotnetfiddle.net/GoMMhy

3 Comments

Thanks so much for the effort, but that is when there is only a single form in the view. I'm looking for a view with two forms and therefore two ValidationSummary()'s so I need to reference just one of those ValidationSummary()'s
Ok, I got it cleary now, some think like this stackoverflow.com/questions/5342427/… , right ?
Similar, but I want to thrown the error on page load based on a querystring value, not when the submit button has been clicked.

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.