0

I have the following code that contains validation:

<div id="ContactDialog" style="display: none;">

    @Html.LabelFor(model => model.subject)
    @Html.TextBoxFor(model => model.subject, new { @class = "AboutControls" })
    @Html.ValidationMessageFor(model => model.subject)
    <br /><br />
    @Html.LabelFor(model => model.from)
    @Html.TextBoxFor(model => model.from, new { @class = "AboutControls" })
    @Html.ValidationMessageFor(model => model.from)
    <br /><br />
    @Html.LabelFor(model => model.body)
    @Html.TextAreaFor(model => model.body, new { @class = "AboutControls" })
    @Html.ValidationMessageFor(model => model.body)
    <hr />
    <br /><br />   
</div>

This code is executed from JQuery ajax code. Because of this, the validation is not triggered. All the fields are required, but since they are not called through an <input type="submit" value="..." /> the validation is not triggered, and they will go through even when they are null, resulting in an exception. How can I make sure the validation is triggered even when no using an <input ... />?

user1960836
  • 1,732
  • 7
  • 27
  • 47
  • possible duplicate of [Use ASP.NET MVC validation with jquery ajax?](http://stackoverflow.com/questions/14005773/use-asp-net-mvc-validation-with-jquery-ajax) – rism Mar 07 '15 at 23:27
  • Are these controls inside a `
    ` element (which is necessary for the `data-val-*` attributes to be rendered and used in conjuction with `jquery.validate.unobtrusive`)
    –  Mar 07 '15 at 23:34
  • Yes they are in a BeginForm – user1960836 Mar 07 '15 at 23:40
  • 1
    Then in that case, you must not have the relevant jquery files included (or your creating the form dynamically in which case you need to re-parse the validator) –  Mar 07 '15 at 23:53
  • Yes I am creating the form dynamically. How can I re-parse the validator? I have tried including: manually at the bottom of the page that creates the form. Normally these 2 tags are also found in the _Layout.cshtml – user1960836 Mar 08 '15 at 09:38
  • @user1960836, [this answer](http://stackoverflow.com/questions/26542509/validate-dynamically-added-fields/26542591#26542591) shows how to reparse the validator, although it looks like you could just include the form in the view initially (inside a hidden element) –  Mar 09 '15 at 12:29

0 Answers0