Click here to Skip to main content
11,927,711 members (54,269 online)
Rate this:
Please Sign up or sign in to vote.
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

    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

    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;
        return Json(flagResult);

And my View

@using (Html.BeginForm()) {
Posted 24-Jul-12 17:18pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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...
Thanh Hoang 26-Jul-12 22:25pm
Thanks for your reply! I tried your solution but It still not get firing.
PratapReddyP 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...
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!
PratapReddyP 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=>, 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!
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

There is a bug in MVC3 related to unobtrusive validation on dropdownlist. Please reference to this[^] 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
@Html.DropDownList("CategoryID", String.Empty)
with this
@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[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 9 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100