I am using ASP.NET MVC3 and EF 4.1 I have two DropDownList in my Model, It is required and not duplicated too. And I want the Remote validate function: ValidateDuplicateInsert get firing when user submit data. But I can NOT get the ValidateDuplicateInsert function firing. What am I wrong? It ugent, please help! Thank you so much!
My Model
[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; }
The ValidateDuplicateInsert function in my CMAndOrg controller
[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);
}
And my View
@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>
}