我有这些变量:做一个必填字段,如果其他字段值
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为必填字段如果在其他作品中,如果DropDownList的文本不是“ - Select - ”,那么IF BossId有一个值,并且我想进行验证在控制器中,使用JavaScript检查与特定销售商(Boss)的入场日期相关的头衔日期),我该如何做到这一点?
什么是错的 “工作正常” 的事情吗? –
我不问脚本1,我只是显示我的代码,我正在寻找解决方案脚本2和一个脚本示例,以验证(在控制器)与JS。你现在做了吗?顺便说一句,我会编辑.. – developer033