它不能在多个字段上进行远程验证

时间:2018-04-14 21:46:45

标签: jquery html asp.net-mvc

我已根据年份和部分字段中this问题的答案对多个字段应用了远程验证,以确保用户不会在同一年输入相同的部分,但未显示任何验证消息

模型(从数据库自动生成,但我添加了[Remote][Required]

[Required]
public Nullable<short> Year { get; set; }
[Required]
[Remote("IsSectionAvailable", "Test", AdditionalFields = "Year", ErrorMessage = "Value is not valid")]
public string Section { get; set; }

控制器

中的功能
[AllowAnonymous]
[HttpPost]
public ActionResult IsSectionAvailable(string Section,short Year)
{
    try
    {
        return Json(!IsSectionExists(Section,Year));
    }
    catch (Exception ex)
    {
        return Json(false);
    }
}
ITDBEntities1 db = new ITDBEntities1();

private bool IsSectionExists(string Section, short Year)
{
    // Query for all projects with section 
    var project = from s in db.SeniorProject
                where s.Section==Section
                select s;
    var Y = from s in db.SeniorProject
                  where s.Year == Year
                  select s;
    if (project.Any()&&Y.Any())
    {
        return true;
    }
    return false;
}

查看

中的表单
@model Testinggggg.Models.SeniorProject
....
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true);

    @Html.LabelFor(model => model.Section)
    @Html.EditorFor(model => model.Section)
    @Html.ValidationMessageFor(model => model.Section)

    @Html.LabelFor(model => model.Year)
    @Html.EditorFor(model => model.Year)
    @Html.ValidationMessageFor(model => model.Year)
}

视图包含

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

0 个答案:

没有答案