Ajax Loader未在MVC远程验证中触发

时间:2015-01-26 12:26:56

标签: jquery asp.net-mvc validation data-annotations

我正在进行MVC远程验证,我需要检查用户名是否存在。我需要在验证期间显示一个ajax加载器。

我的观点

  @model Grid.Models.CategoryViewModel

  @using GridMvc.Html

 @{
  Layout = "~/Views/Shared/_Layout.cshtml";
  }
    <script>

    $(function () {
        $("#loader").hide();
        $('form').ajaxStart(function () {
            $("#loader").show();
        }).ajaxStop(function () {
          $("#loader").hide();
      });
    })

        @using (Html.BeginForm()) 
    {
         @Html.ValidationSummary(true)

        <img id="loader" src="~/Content/images/loading.gif" />
        <fieldset id="addField">
            <div class="row">
                @Html.LabelFor(x=>x.CategoryToInsert.CatagoryName)
                @Html.TextBoxFor(m => m.CategoryToInsert.CatagoryName, new { maxlength = 50 })
                @Html.ValidationMessageFor(x => x.CategoryToInsert.CatagoryName)
            </div>
        </fieldset>

        <div id="success-message" style="display: none;">
            This record is successfully saved!!
        </div>
        <div>
            <input type="submit" value="Save" />
        </div>
    }

我的控制器

    [HttpPost]
    public JsonResult IsCategoryUnique(Category category)
    {
        Thread.Sleep(10000);
        return Json("Category is unavailable", JsonRequestBehavior.AllowGet); 
    }

和模型

       public class CategoryViewModel
{
    public Category CategoryToInsert { get; set; }
    public IEnumerable<Category> CategoriesToShow { get; set; }
}
public class Category
{
    public int CatagoryID { get; set; }

    [Remote("IsCategoryUnique", "Client", HttpMethod = "POST")]
    public string CatagoryName { get; set; }

    [Required(ErrorMessage = "First Name is required")]
    public string Color { get; set; }

    [Required(ErrorMessage = "First Name is required")]
    [Display(Name = "PrintTemplate")]
    public string PrintTemplate { get; set; }

    [Required(ErrorMessage = "First Name is required")]
    public string Language { get; set; }
}

远程验证被触发正常,但ajaxStart和Stop不会触发。我哪里错了?

看到这些,但同样的答案仍然不适合我

How to show the ajax loader image while remote validating using data annotation in asp.net mvc3

0 个答案:

没有答案