验证2个文本框

时间:2014-03-22 16:04:19

标签: c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-validation

我需要验证文本框,如果两者都验证失败,则显示错误消息。

在我的Models类中我有:

        [Remote("IsDateAvailable", "Home", AdditionalFields = "initial")]
        public Nullable<System.DateTime> Date { get; set; }

        [Remote("IsFirstAvailable", "Home", AdditionalFields = "initial")]
        public Nullable<int> First { get; set; }

并在控制器中:

 public ActionResult IsDateAvailable(DateTime Date, string initial)
        {
            if (!db.data.Any(x => x.Date == Date))

                return Json(true, JsonRequestBehavior.AllowGet);
            return Json(string.Format("{0} is not available", Date), JsonRequestBehavior.AllowGet);
    }
 public ActionResult IsFirstAvailable(int First, string initial)
    {
        if (!db.data.Any(x => x.First == First))

                return Json(true, JsonRequestBehavior.AllowGet);
        return Json(string.Format("{0} is not available", First), JsonRequestBehavior.AllowGet);
    }

所以这就像这样。 但我需要检查是否在特定日期输入First,这将检查First是否在db。

这是观点:

       <div class="form-group">
        @Html.LabelFor(model => model.Date, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.Date, new {@id ="date" })
            @Html.ValidationMessageFor(model => model.Date)
        </div>

        <div class="form-group">
        @Html.LabelFor(model => model.First, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
        @Html.TextBoxFor(model => model.First, new { @id = "id1", @disabled = "disabled" })
            @Html.ValidationMessageFor(model => model.First)
            @Html.CheckBoxFor(model => model.Checked1, new { @class = "class1" })
        </div>
    </div>

0 个答案:

没有答案