This is a migrated thread and some comments may be shown as answers.

DropDownList not passing values to my controller

15 Answers 3704 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Avery
Top achievements
Rank 1
Avery asked on 29 Sep 2013, 05:53 PM
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.

15 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 30 Sep 2013, 08:47 AM
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!
0
Avery
Top achievements
Rank 1
answered on 04 Oct 2013, 08:32 PM
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?  
0
Georgi Krustev
Telerik team
answered on 07 Oct 2013, 08:55 AM
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!
0
Avery
Top achievements
Rank 1
answered on 08 Oct 2013, 11:17 PM
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.

0
Georgi Krustev
Telerik team
answered on 11 Oct 2013, 07:41 AM
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!
0
Avery
Top achievements
Rank 1
answered on 11 Oct 2013, 07:58 PM
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!

0
Georgi Krustev
Telerik team
answered on 14 Oct 2013, 02:18 PM
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!
0
Alejandro
Top achievements
Rank 1
answered on 11 May 2016, 04:23 PM
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
0
Hristo Valyavicharski
Telerik team
answered on 13 May 2016, 07:58 AM
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
0
Alejandro
Top achievements
Rank 1
answered on 07 Nov 2016, 11:26 PM
Hello, I could solve it greetings
0
umer
Top achievements
Rank 1
answered on 15 Nov 2017, 01:04 PM
<p>I am Creating Cascade dropdownlist its working well but its Giving ProvinceId 0 to my controller </p> <p>@(Html.Kendo().DropDownListFor(model => model.ProvinceId)<br>
                     .OptionLabel("--Select Province--")<br>
                    <br>
                     .HtmlAttributes(new { style = "width:283px" })<br>
                     .DataTextField("ProvinceName")<br>
                     .DataValueField("ProvinceId")<br>
<br>
                          .DataSource(source =><br>
                          {<br>
                              source.Read(read =><br>
                              {<br>
                                  read.Action("GetCascadeProvince", "Student")<br>
                                  .Data("filterCountries");<br>
                              }).ServerFiltering(true);<br>
                          })<br>
                          .Enable(false)<br>
                          .AutoBind(true)<br>
                          .CascadeFrom("CountryId")<br>
                    )</p><br>
0
Ivan Danchev
Telerik team
answered on 17 Nov 2017, 11:28 AM
Hello Umer,

Could you elaborate more on the following points:
  • Which particular action in the controller are you trying to access ProvinceId in? Could you post the action's declaration?
  • How is this action called?
  • Could you post your filterCountries function? This will show us the parameters you are sending to the GetCascadeProvince action.

Regards,
Ivan Danchev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Rayan
Top achievements
Rank 1
answered on 19 Mar 2018, 02:21 PM
Thank you, I had the same issue
0
Diana
Top achievements
Rank 1
answered on 19 Jun 2018, 03:23 PM

Is there some reason why the model (ienumerable) does not pass back to the controller the selected value     

 

Here is the controller definition:

  [HttpPost]
  public ActionResult FEEmployee_Read(List<SelectPayWeek>SelectedPayWeek)

View:

@model IEnumerable<TimeSheetEntryForManagers.Models.SelectPayWeek>
@*@model IEnumerable<MvcApplication1.Models.Product>*@
@{
    ViewBag.Title = "Select Pay Week";
}
<script type="text/javascript">
</script>
<style>
    #payweekDropDownList {
        width: 1000px !important;
    }
</style>
@using (Html.BeginForm("FEEmployee_Read", "FETimeEntry", FormMethod.Post))
//, new { payweek = Model, onsubmit = "validate()" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

<div>
    <h4> Select a pay week </h4>
 
   
    @(Html.Kendo().DropDownList()
                                      .Name("ddlPayWeek")
                                       .DataTextField("PayWeekStartDate")
                                      .DataValueField("TMPayWeekID")
                                      .BindTo(Model)
                                      .SelectedIndex(0)
                                      .HtmlAttributes(new { style = "width: 100%" })
    )


0
Ivan Danchev
Telerik team
answered on 21 Jun 2018, 10:09 AM
Hello,

Are you able to pass other field values or the value of the standard MVC @Html.DropDownList helper to the controller or you have an issue only with the Kendo UI DropDownList? See the following thread which demonstrates the DropDownList's configuration, how its value is submitted with the form and how it can be accessed in the controller.

Regards,
Ivan Danchev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
DropDownList
Asked by
Avery
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Avery
Top achievements
Rank 1
Alejandro
Top achievements
Rank 1
Hristo Valyavicharski
Telerik team
umer
Top achievements
Rank 1
Ivan Danchev
Telerik team
Rayan
Top achievements
Rank 1
Diana
Top achievements
Rank 1
Share this question
or