Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
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

C#
[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

C#
[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

C#
@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>
}
Posted

1. When you write [Remote] on CMID, you don't need to mention it in Additional Fields.... by default, CMID will be passed to action method.
2. Change your return statement as below, it will work.
return Json(flagResult, JsonRequestBehavior.AllowGet);

let me know how it worked...
 
Share this answer
 
Comments
Thanh Hoang 26-Jul-12 22:25pm    
Thanks for your reply! I tried your solution but It still not get firing.
[no name] 26-Jul-12 22:31pm    
try removing HttpMethod = "Post" attribute from remote and remove [HttpPost] attribute on method.

i have a working sample of it...
http://pratapreddypilaka.blogspot.in/2011/08/mvc-remote-validation.html
Thanh Hoang 26-Jul-12 23:25pm    
I removed HttpMethod = "Post" attribute from remote and remove [HttpPost] attribute on method, but It still not get firing.
Your example is remote validate on TextBox not DropDownList.
Can you do an example for DropDownList?
Thank you so much!
[no name] 6-Aug-12 12:13pm    
Gotcha . . . in the code given above, the dropdown you are using is a simple one which is not bound to model. Try changing it to @Html.DropDownFor(m=>m.property), it will fire.

As a last resort, you can even validate the selection using a jQuery calling the same Ajax Action method. U can use $("#CMID").change(function(){});
Thanh Hoang 6-Aug-12 22:22pm    
I tried but I am get no lucky, it still not working.
Many thanks for trying help!
There is a bug in MVC3 related to unobtrusive validation on dropdownlist. Please reference to this http://aspnet.codeplex.com/workitem/7629[^] link for more detail explaination.

Briefly, you can't use the same name for category collection and category field, so just change your collection name and update following line in your view
C#
@Html.DropDownList("CategoryID", String.Empty)

with this
C#
@Html.DropDownListFor(model => model.CategoryID, new SelectList((System.Collections.IEnumerable)ViewData["Categories"], "Value", "Text"))


Thanks again Henry He

Original link
ASP.NET MVC3 Validation Basic[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900