我正在使用 ASP.NET MVC3 和 EF 4.1 我的模型中有两个 DropDownList,它是必需的,也不重复。我想要远程验证功能:ValidateDuplicateInsert 在用户提交数据时触发。但我无法触发 ValidateDuplicateInsert 函数。我哪里错了?
我的模特
[Key]
public int CMAndOrgID { get; set; }
[Display(Name = "CM")]
[Required(ErrorMessage = "CM is required.")]
[Remote("ValidateDuplicateInsert", "CMAndOrg", HttpMethod = "Post", AdditionalFields = "CMID, OrganizationID", ErrorMessage = "CM is assigned to this Organization.")]
public int? CMID { get; set; }
[Display(Name = "Organization")]
[Required(ErrorMessage = "Organization is required.")]
public int? OrganizationID { get; set; }
public virtual CM CM { get; set; }
public virtual Organization Organization { get; set; }
我的 CMAndOrg 控制器中的 ValidateDuplicateInsert 函数
[HttpPost]
public ActionResult ValidateDuplicateInsert(string cmID, string orgID)
{
bool flagResult = true;
foreach (CMAndOrg item in db.CMAndOrgs)
{
if (item.CMID.ToString() == cmID && item.OrganizationID.ToString() == orgID)
{
flagResult = false;
break;
}
}
return Json(flagResult);
}
我的观点
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>CMAndOrg</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CMID, "CM")
</div>
<div class="editor-field">
@Html.DropDownList("CMID", String.Empty)
@Html.ValidationMessageFor(model => model.CMID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.OrganizationID, "Organization")
</div>
<div class="editor-field">
@Html.DropDownList("OrganizationID", String.Empty)
@Html.ValidationMessageFor(model => model.OrganizationID)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}