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?

spaceduk
  • 115
  • 1
  • 10
  • 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. – GSerg Oct 10 '15 at 18:20
  • for better understanding can u please explain ur scenario in a dotnetfiddle.net – Kishore Sahasranaman Oct 10 '15 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. – spaceduk Oct 10 '15 at 18:36
  • Ok Good, still for my understanding i just created a fiddle .. https://dotnetfiddle.net/GoMMhy , is this what u are expecting ? – Kishore Sahasranaman Oct 10 '15 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. – spaceduk Oct 11 '15 at 11:31
  • You need to have separate models for the partial views, most likely backed up with two [child-action-only methods](http://stackoverflow.com/q/10253769/11683) in the controller. Then you call `Html.RenderAction` twice from the main view. – GSerg Oct 11 '15 at 11:48

2 Answers2

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.

spaceduk
  • 115
  • 1
  • 10
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

Kishore Sahasranaman
  • 4,013
  • 3
  • 24
  • 50
  • 2
    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 – spaceduk Oct 10 '15 at 19:37
  • Ok, I got it cleary now, some think like this http://stackoverflow.com/questions/5342427/specify-validation-summary-on-multiple-forms , right ? – Kishore Sahasranaman Oct 11 '15 at 05:45
  • Similar, but I want to thrown the error on page load based on a querystring value, not when the submit button has been clicked. – spaceduk Oct 11 '15 at 11:10