Click here to Skip to main content
Rate this: bad
good
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
 
[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>
}
Posted 24-Jul-12 17:18pm
Rate this: bad
good
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...
  Permalink  
Comments
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...
http://pratapreddypilaka.blogspot.in/2011/08/mvc-remote-validation.html
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=>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 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
good
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 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
@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[^]
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 9 Aug 2012
Copyright © CodeProject, 1999-2014
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