如果其他字段有值,则创建必填字段
Make a required field if the other field has value
我有这些变量:
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);
}
如果 BossId 有值,我想将 HeadShipDate 设为必填字段,在其他作品中,如果 DropDownList 的文本不是“-- Select --”,并且我想进行验证(我在 controller 中设置的那个条件,用那个特定卖家(Boss)的 admissionDate 检查领导日期)JavaScript,我该怎么做?
在您的视图模型上实施 IValidatableObject 可以更好地控制像这样的临时验证规则。
例子
public class Department : IValidatableObject
{
public int? BossId { get; set; }
public DateTime? HeadShipDate { get; set; }
...
public IEnumerable<ValidationResult> Validate( ValidationContext validationContext )
{
if( BossId.HasValue && !HeadShipDate.HasValue )
{
yield return new ValidationResult( "HeadShipDate is required if BossId is selected.", new[] { "HeadShipDate" } );
}
}
}
那么您只需要在您的控制器操作中检查 ModelState.IsValid
。
注意:这仅将服务器端验证应用于 ModelState。如果您还需要客户端验证,您将需要实现一个 Javascript 函数,该函数在提交表单之前执行类似的操作。
示例:
$('form').validate({
rules: {
HeadShipDate: {
required: {
depends: function (element) {
return $("#BossId").val() != '';
}
}
}
},
messages: {
HeadShipDate: {
required: "Please specify the Headship Date"
}
}
});
我有这些变量:
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);
}
如果 BossId 有值,我想将 HeadShipDate 设为必填字段,在其他作品中,如果 DropDownList 的文本不是“-- Select --”,并且我想进行验证(我在 controller 中设置的那个条件,用那个特定卖家(Boss)的 admissionDate 检查领导日期)JavaScript,我该怎么做?
在您的视图模型上实施 IValidatableObject 可以更好地控制像这样的临时验证规则。
例子
public class Department : IValidatableObject
{
public int? BossId { get; set; }
public DateTime? HeadShipDate { get; set; }
...
public IEnumerable<ValidationResult> Validate( ValidationContext validationContext )
{
if( BossId.HasValue && !HeadShipDate.HasValue )
{
yield return new ValidationResult( "HeadShipDate is required if BossId is selected.", new[] { "HeadShipDate" } );
}
}
}
那么您只需要在您的控制器操作中检查 ModelState.IsValid
。
注意:这仅将服务器端验证应用于 ModelState。如果您还需要客户端验证,您将需要实现一个 Javascript 函数,该函数在提交表单之前执行类似的操作。
示例:
$('form').validate({
rules: {
HeadShipDate: {
required: {
depends: function (element) {
return $("#BossId").val() != '';
}
}
}
},
messages: {
HeadShipDate: {
required: "Please specify the Headship Date"
}
}
});