DropDownList not passing values to my controller

11 posts, 0 answers
  1. Avery
    Avery avatar
    8 posts
    Member since:
    Aug 2010

    Posted 29 Sep 2013 Link to this post

    Hello. I am hoping to get some help here, I'm stuck. I am trying to get the value from my dropdown list to my controller to save back to the db but the value doesn't seem to come back with the post. 

    My Model
    namespace MDT.Models
    {
        public class Vehicle
        {
            [Key]
            public int VehicleID { get; set; }
     
            public int UserID { get; set; }
     
            [Required]
            [StringLength(50)]
            public string Name { get; set; }
     
            public int MakeID { get; set; }
            public int ModelID { get; set; }
     
            [DisplayName("Color")]
            public int ColorID { get; set; }
     
            [Required]
            [StringLength(4)]
            public string StickerKey { get; set; }
     
            [DefaultValue(true)]
            public Boolean IsActive { get; set; }
     
            public virtual CarColor CarColor { get; set; }
        }
     
    }

    My Controller action for the udpate
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Vehicle vehicle)
    {
        if (ModelState.IsValid)
        {
            db.Entry(vehicle).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(vehicle);
    }
    My Kendo drop down list definition in the cshtml
    <div class="editor-label">
        @Html.LabelFor(model => model.ColorID)
    </div>
    <div class="editor-field">
        @*@Html.EditorFor(model => model.ColorID)*@
        @Html.ValidationMessageFor(model => model.ColorID)
     
        @(Html.Kendo().DropDownListFor(model => model.ColorID)
            .BindTo(ViewData["CarColors"] as SelectList)
            .Name("kendocarcolor")
            .Value(Model.ColorID.ToString()) 
            .OptionLabel("Select a Color")             
        )
    </div>

    When I perform the save for the above, the ColorID comes back at zero. Shouldn't it be getting passed back with the updated model for the post? I also have noticed that my validator for the ColorID is not working for the Kendo Dropdown list but if I uncomment the EditorFor, it does work on that control.

    If you could please help me with the following it would be greatly appreciated:

    1) Why doesn't the dropdownlist come back with my updated model value to my controller?
    2) Any ideas why the validator doesn't work?
    3) Did I populate the dropdownlist the most efficent way using ViewData? How would you do it?

    THANK YOU very much in advance.

  2. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 30 Sep 2013 Link to this post

    Hello Avery,

     
    Widget's value is not bound to the ColorID property, because its name is overriden using this line:

    .Name("kendocarcolor")
    When this line is called, the name of the name of the widget is changed from "ColorID" to "kendocarcolor". Thus, the ASP.NET MVC will not be able to map the posted value to the ColorID property. Check this link for more information.

    Regards,
    Georgi Krustev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Avery
    Avery avatar
    8 posts
    Member since:
    Aug 2010

    Posted 04 Oct 2013 Link to this post

    Thank you Georgi. That was it. Stupid mistake. Even though I made that change, my required data annotation is still not tied to the Kendo drop down list.

    Any ideas on that one?  
  5. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 07 Oct 2013 Link to this post

    Hello again Avery,

     
    I am not exactly sure where could be the cause of the issue. If both the EditorFor extension method and the Kendo DropDownListFor are used on the page then only the first one will get the unobtrusive data annotation attributes. I suppose that this is not the case at your implementation. Hence I will need a simple test project, which reproduces the issue. Thus I will check the problem locally and advice you further.

    Regards,
    Georgi Krustev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Avery
    Avery avatar
    8 posts
    Member since:
    Aug 2010

    Posted 08 Oct 2013 Link to this post

    Thanks Georgi. Here is a stripped down version of my project, the whole project was too big. Let me know if you can't get it to work. 

    My problem is on the create vehicle, if you leave the dropdown blank you will get an error and no validation is happening what so ever.

    THANK YOU.

  7. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 11 Oct 2013 Link to this post

    Hello again Avery,

     
    Thank you the attached files. Unfortunately, I was not able to get the project to work. One assumption about the missing validation. If you are using jQuery Validate framework (I was not able to find any jquery.validate.js though) then you will need to set ignore option to empty string, as it does not validate hidden elements since version 1.9.0:
    [_Layout.cshtml]

    <script>
        $.validator.defaults.ignore = "";
    </script>

    If this is not the case the I will ask you to send us a simple runnable test project. Thus we will be able to investigate the case locally and find a resolution to the problem fast. 

    Regards,
    Georgi Krustev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  8. Avery
    Avery avatar
    8 posts
    Member since:
    Aug 2010

    Posted 11 Oct 2013 Link to this post

    Ok here is the full project. I have to do it from a download because it's too big to upload. You can download it at:

    https://www5.newportbeachca.gov/webmedia/kendomdt.zip

    To duplicate my problem:

    1) Run the project
    2) Click on Vehicles in the top menu (that should take you to the create screen)
    3) Try to submit the form without entering in any values.

    The Kendo Dropdownlist seems to kill validation for any other control as well as pull an error without doing any server side validation. My understanding is that server side validation should at least happen even if the client side is not happening. I'm sure it's something stupid that I'm doing wrong. Thank you!

  9. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 14 Oct 2013 Link to this post

    Hello Avery,


    Thank you for the attached test project. I was able to run it and it actually worked as expected. The page loads correctly and on post, the server validation reports that the model is not valid. Check this screencast for more information. Probably I am missing something?


    Regards,
    Georgi Krustev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  10. Alejandro
    Alejandro avatar
    2 posts
    Member since:
    Feb 2016

    Posted 11 May Link to this post

    Hi, I have a same problem, I want pass data from dropdownlistfor to my controller but data is empty when arrive to controller Help me Please
  11. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 13 May Link to this post

    Hi Alejandro,

    Can you paste your code? 

    Thanks.

    Regards,
    Hristo Valyavicharski
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  12. Alejandro
    Alejandro avatar
    2 posts
    Member since:
    Feb 2016

    Posted 07 Nov in reply to Hristo Valyavicharski Link to this post

    Hello, I could solve it greetings
Back to Top
UI for ASP.NET MVC is VS 2017 Ready