我有這些變量:做一個必填字段,如果其他字段值
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