5

Hi I am developing a web application in MVC 4 Asp.Net C#. I have a design which showing all the error at top of the page. But when using Data Annotations I am unable to show validation summary outside the form. Can any one please help me out to overcome this issue thans in advance. Following is my Senario

My forms look like this

    @model ChrisFinnerty.Models.LocalPasswordModel
    @{
        ViewBag.Title = "Change Password";
    }

    @using (Html.BeginForm("ChangePassword", "Account", FormMethod.Post, new {@id = "FormChangePassword", @role = "form", @class = "form-horizontal well"}))
    {

        @Html.ValidationSummary()

        <h2>Change Password</h2>
        @Html.AntiForgeryToken()

        @Html.PasswordFor(m => m.OldPassword, new {@class = "form-control"})

        @Html.PasswordFor(m => m.NewPassword, new {@class = "form-control"})

        @Html.PasswordFor(m => m.ConfirmPassword, new {@class = "form-control"})

        <button type="submit" class="btn btn-default">Change Password</button>
        <button type="button" class="btn btn-default" onclick="window.history.go(-1) ">Cancel</button>
     }

But I want like that show validation summary outside the form in notification div

        @model ChrisFinnerty.Models.LocalPasswordModel
        @{
            ViewBag.Title = "Change Password";
        }

        @Html.ValidationSummary() //*****Just want to add it here its not working****

        @using (Html.BeginForm("ChangePassword", "Account", FormMethod.Post, new {@id = "FormChangePassword", @role = "form", @class = "form-horizontal well"}))
        {
            <h2>Change Password</h2>
            @Html.AntiForgeryToken()

            @Html.PasswordFor(m => m.OldPassword, new {@class = "form-control"})

            @Html.PasswordFor(m => m.NewPassword, new {@class = "form-control"})

            @Html.PasswordFor(m => m.ConfirmPassword, new {@class = "form-control"})

            <button type="submit" class="btn btn-default">Change Password</button>
            <button type="button" class="btn btn-default" onclick="window.history.go(-1) ">Cancel</button>
         }
BPX
  • 888
  • 1
  • 8
  • 20
Waqas Idrees
  • 1,443
  • 2
  • 17
  • 36
  • Just move the div into the form and your problem is solved... Not sure what the difference is. – Simon Whitehead Jan 23 '14 at 04:54
  • You can also achieve this thing using CSS. – Jayesh Goyani Jan 23 '14 at 04:55
  • Form is at bottom of page and notification div is at the top of page. its design limitation I cant add notification div into form its disturb the layout of page :'( – Waqas Idrees Jan 23 '14 at 04:57
  • DataAnootations will bind the error messages to those particular input elements(so it will be shown with the input element itself). If all you want to show is a list of errors try using a error message pop up http://www.jquery4u.com/plugins/10-jquery-alert-windows-prompts/ you can use something from these – Rajshekar Reddy Jan 23 '14 at 05:08
  • Dear I just want to show @Html.validationsummary() outside the form – Waqas Idrees Jan 23 '14 at 05:33
  • @WaqasIdrees have u seen inbuilt application given in VS2010 for mvc3 in it for LogOn.cshtml page there is exact implementation as u want i.e. Valicationsummary is outside form – Nitin Varpe Jan 23 '14 at 05:47
  • Can you all people please check it now I improve the layout of my question – Waqas Idrees Jan 23 '14 at 06:09
  • @Nitin Varpe Let me check – Waqas Idrees Jan 23 '14 at 06:11
  • @NitinVarpe you right but they implement with the static message like this @Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") – Waqas Idrees Jan 23 '14 at 06:17
  • You can add `ModelState.AddModelError` at controller side and add any erros you want. Have u seen controller action for LogOn – Nitin Varpe Jan 23 '14 at 06:21
  • @NitinVarpe I added there that why its working inside the form – Waqas Idrees Jan 23 '14 at 06:32
  • didnt get u! have u tested in that demo app – Nitin Varpe Jan 23 '14 at 06:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45871/discussion-between-waqas-idrees-and-nitin-varpe) – Waqas Idrees Jan 23 '14 at 06:36
  • Did you ever get this question answered? I have the same issue with MVC 4.. Sure I can add @Html.ValidationSummary() to each form, but it's silly. I want to design validation message handling once and have it just work rather than putting @Html.ValidationSummary() in each form – eaglei22 Mar 17 '17 at 14:39
  • @eaglei22 You can achieve this only by using JavaScript – Waqas Idrees Mar 17 '17 at 21:31

1 Answers1

-1

Just add @Html.ValidationSummary() out side the form. I had same problem see this. MVC3 ValidationSummary Control

See this example http://mahaafifi.blogspot.in/2011/12/multiple-validation-summary-at-single.html

Community
  • 1
  • 1
Ajay
  • 6,418
  • 18
  • 79
  • 130
  • 1
    But when I add the @Html.ValidationSummary() tag outside the form its shows nothing Only showing error when we write this inside form tag – Waqas Idrees Jan 23 '14 at 05:56
  • I did same implementation. it works from my side. check that link in my ans. – Ajay Jan 23 '14 at 05:59
  • I checked but not worked and in that link it is not mentioned that we are able to write validation summary outside the form. I checked at my side but its not working – Waqas Idrees Jan 23 '14 at 06:02
  • @WaqasIdrees Why you are want it outside the form ? I checked my code it is inside the form. – Ajay Jan 23 '14 at 06:29
  • 1
    Because in all pages I show the error message at the top of the page and my some form is at bottom of page but I want to show error at top page just like other pages in my application – Waqas Idrees Jan 23 '14 at 06:35
  • you can add it above `

    change password

    `
    – Ajay Jan 23 '14 at 06:38
  • 1
    you are not understanding my requirement yet I want to add @html.Validationsummay any where else in my page any where not just right top of form any where – Waqas Idrees Jan 23 '14 at 06:44