【发布时间】:2015-05-28 15:50:06
【问题描述】:
我有这些变量:
public int? BossId { get; set; }
public DateTime? HeadShipDate { get; set; }
我的看法:
<div class="form-group">
@Html.LabelFor(model => model.BossId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("BossId", null, "-- Select --", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.BossId, "", new { @class = "text-danger" })
</div>
</div>
<div id="divDate" class="form-group">
@Html.LabelFor(model => model.HeadShipDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HeadShipDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.HeadShipDate, "", new { @class = "text-danger" })
</div>
</div>
脚本(如果 BossId 有值,我试图使之成为必需):
<script type="text/javascript">
HeadShipDate: {
required: {
depends: function(element){
return $("#BossId").text() != '-- Select --';
}
}
}
</script>
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Acronym,Percentage,BossId,HeadShipDate")] Department department)
{
Seller boss = db.Sellers.Find(department.BossId);
if (boss.AdmissionDate > department.HeadShipDate)
ModelState.AddModelError(string.Empty, "Headship Date (" + department.HeadShipDate.Value.ToShortDateString() + ") must be greater than the Admission Date (" + boss.AdmissionDate.ToShortDateString() + ") of boss");
else if (ModelState.IsValid)
{
boss.DeptId = department.Id;
db.Departments.Add(department);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.BossId = new SelectList(SellersNonBosses(), "Id", "Name", department.BossId);
return View(department);
}
我想让 HeadShipDate 必填字段 IF BossId 有一个值,在其他作品中,如果 DropDownList 的文本不是“-- Select --”,并且我想进行该验证(我在控制器,使用 JavaScript 检查特定卖家(老板)的admissionDate 日期,我该怎么做?
【问题讨论】:
-
“工作正常”的东西有什么问题?
-
我不是在询问脚本 1,我只是在显示我的代码,我正在寻找脚本 2 的解决方案和一个脚本示例,以使用 JS 进行验证(在控制器中)。你现在明白了吗?顺便说一句,我会编辑..
标签: javascript c# jquery controller asp.net-mvc-5.2