我使用基于MVC 4的不显眼属性验证。我有一个非常标准的输入表单。一开始所有字段都是空的。我有选择(下拉列表)的问题。它所需的字段和故障情况如下:
如果用户在从下拉列表中选择值后没有点击外部,则无法通过验证,直到两次单击提交按钮。虽然第一次点击不是点击,但只有#34;焦点"丢失在下拉列表中。
有没有办法用jQuery或某种方式强制页面在选择值后失去对下拉列表的关注。我尝试在点击提交按钮和下拉列表上的选择事件上执行jQuery的validate()。两者都没有结果。
我的部分视图如下:
@model EditVM
@{
HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}
@using (@Html.BeginForm(Model.ActionName, Model.ControllerName, FormMethod.Post, new { id = "editForm" }))
{
<div class="sth_form">
<div class="frm_row">
@Html.LabelFor(model => model.FIELD1)
<div class="frm_row_input">
@Html.TextBoxFor(model => model.FIELD1)
@Html.ValidationMessageFor(model => model.FIELD1)
</div>
</div>
<div class="frm_row">
@Html.LabelFor(model => model.FIELD2)
<div class="frm_row_input">
@Html.TextBoxFor(model => model.FIELD2)
@Html.ValidationMessageFor(model => model.FIELD2)
</div>
</div>
<div class="frm_row" id="contractorHeaderRow">
@Html.LabelFor(model => model.CONTRACTOR)
<div class="frm_row_input">
</div>
</div>
<div class="frm_row" id="contractorRow">
<div class="frm_row_input" id="contractorDropDownList">
@Html.DropDownListFor(model => model.CONTRACTOR, Model.Contractors)
@Html.ValidationMessageFor(model => model.CONTRACTOR)
</div>
</div>
</div>
<div class="text_rgt">
<input type="submit" name="Save" value="@Model.LabelSave" />
<input type="submit" name="Reset" value="@Model.LabelReset" />
</div>
}
我的观点:
@model EditVM
@Html.Partial(MVC.Shared.Views.Sth._Edit, Model)
我的控制器操作:
[HttpGet]
public virtual ActionResult Insert()
{
#region Preparing model
var model = new EditVMBuilder().BuildInsert();
PrepareBaseProperties(model);
LoadDropDownListsForSth(model);
#endregion
return View(model);
}
[HttpPost]
public virtual ActionResult Insert(EditVM model)
{
if (Request.Form["Save"] != null)
{
if (ModelState.IsValid)
{
#region Save
Mapper.CreateMap<EditVM, SthModel>();
SthModel sth = new SthModel();
Mapper.Map(model, sth);
_dataAccess.SthAdd(sth);
_dataAccess.SaveChanges();
#endregion
#region Show result
var resultMessage = string.Format(
"{0}: {1}",
Resources.ResultMessages.SomeMessage,
model.FIELD1);
model.ResultMessage = resultMessage;
PrepareBaseProperties(model);
#endregion
return View(MVC.Shared.Views._Message, model);
}
}
if (Request.Form["Reset"] != null)
{
ModelState.Clear();
return this.Insert();
}
//Validation
model = new EditVMBuilder().ConfigureInsert(model);
PrepareBaseProperties(model);
LoadDropDownListsForSth(model);
return View(model);
}