2

I am trying to figure out ,why my validation messages are not showing up?

Here is my Razor page

@model MyViewModel
@{
 Layout = "~/Views/Shared/_MainLayout.cshtml";
    var listErrors = ViewBag.listErrors as Array;
}
<section class="selectconfiguration">
    <h3 class="active">Add Rule</h3>
    <div class="con-section">

         @{Html.EnableClientValidation(true);}
        @{Html.EnableUnobtrusiveJavaScript();}
        @using (Html.BeginForm("AddRules", "Aliases", "HttpPost"))
        {


            @Html.ValidationSummary(true)
            <table>
                <tr>
                    <td class="phone-options-label-column">
                        <div class="editor-label">
                            @Html.Label("Alias String:")
                        </div>
                    </td>
                    <td>

                        @Html.EditorFor(m => m.AliasString,new {@id="txtAliasString"})
                    </td>
                    <td> @Html.ValidationMessageFor(m => m.AliasString)</td>

                </tr>
                 <tr>
                    <td class="options-label-column">
                        <div class="editor-label">
                            @Html.Label("Select an Associated Model:")
                        </div>
                    </td>
                    <td>
                       @Html.DropDownList("ddlModels", new SelectList(Model.Models, "Id", "Name", ""), "Unknown", new { @class = "mselectDDContItem" })
                    </td>
                    <td>
                         </td>
                </tr>
                </table>
        }
       <div>
                <input type="submit" value="Add Rule" id="btnAddRule" name="AddRule" />
            </div>
        </div>
    </section>

Here is my ViewModel

  public class MyViewModel
        {
            [Required(ErrorMessage = "alias string required")]
            [StringLength(30, ErrorMessage = "AliasString cannot be larger than 30 characters")]
           public string AliasString { get; set; }      
           public IEnumerable<IParameter> Models { get; set; }
           public string StatusMessage { get; set; }
        }

On clciking the addRules button triggers my jquery clcik event

<script type="text/javascript">
    $(document).ready(function () {

        $("#btnAddRule").click(function (e) {
            e.preventDefault();

            var params = {
                'aliasString': $("#txtAliasString").val(),
                'modelId': $("#ddlModels").val()
            };
            $.ajax({
                url: "/Aliases/AddRules",
                type: "POST",
                dataType: 'json',
                data: JSON.stringify(params),
                async: false,
                cache: false,
                traditional: true,
                contentType: 'application/json',
            }).done(function () {
            }).complete(function () {
            }).success(function (dv) {
            });
        });

    });
    </script>

and I have these script references also on my layout page

<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.treeview.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.selectmenu.js")" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/global.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.nad.session.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")"></script>

I dont understhnad why the validations are not firing. If I click the button with the empty textbox also ,I can see post back happens and modelstate.isValid is returning true to the controller.

6
  • I can see post back happens and modelstate.isValid is returning true to the controller. Under what circumstances? Commented Jan 10, 2014 at 18:51
  • 1
    Have you included the necessary javascript libraries? Commented Jan 10, 2014 at 18:54
  • Try [Required(AllowEmptyStrings = false, ErrorMessage = "...")] Commented Jan 10, 2014 at 18:55
  • @WeTTTT That will make no difference, given that it defaults to false anyway. Commented Jan 10, 2014 at 18:57
  • Try dropping the MicrosoftMvcValidation.js script. Commented Jan 10, 2014 at 19:09

2 Answers 2

4

In my jquery button click function ,I added ,$('#myForm').valid() and then my validation started showing up.

$("#btnAddRule").click(function (e) {


if ($('#myForm').valid()) {



}


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

Comments

0

Just put submit button inside the form and validation would work

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.