Click here to Skip to main content
Rate this: bad
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()) {
    <div class="editor-label">
        @Html.LabelFor(model => model.CMID, "CM")
    <div class="editor-field">
        @Html.DropDownList("CMID", String.Empty)
        @Html.ValidationMessageFor(model => model.CMID)
    <div class="editor-label">
        @Html.LabelFor(model => model.OrganizationID, "Organization")
    <div class="editor-field">
        @Html.DropDownList("OrganizationID", String.Empty)
        @Html.ValidationMessageFor(model => model.OrganizationID)
        <input type="submit" value="Create" />
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 at 26-Jul-12 22:25pm
Thanks for your reply! I tried your solution but It still not get firing.
PratapReddyP at 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 at 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 at 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 at 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
0 OriginalGriff 520
1 Maciej Los 205
2 Suvendu Shekhar Giri 159
3 Peter Leow 140
4 Kornfeld Eliyahu Peter 133
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 8,895
2 Peter Leow 5,044
3 Kornfeld Eliyahu Peter 3,333
4 Maciej Los 2,561

Advertise | Privacy | Mobile
Web04 | 2.8.150327.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