JTunney
JTunney

Reputation: 904

KendoUI-MVC DropDownList populating on PageLoad and Saving selected value in controller

I am able to bind my KendoUI DropDownList but on save I have no idea how to get the actual selected value. I assumed since it was within my razor form it would automatically bind the value.

On save all of my properties are getting set except for this one. How do I make sure in my Edit.cshtml the correct value will be pre-selected and then on save the value will get sent to my POST Action method with all of the other values. Below is how my KendoUI DropDownList is placed in view:

<div class="form-group">
    @Html.LabelFor(model => model.StateId, htmlAttributes: new { @class = "control-label" })
    <br/>
        @(Html.Kendo().DropDownList()
              .Name("ddlStates")
              .HtmlAttributes(new { style = "width: 250px" })
              .DataTextField("Name")
              .DataValueField("Id")
              .DataSource(source =>
              {
                  source.Read(read =>
                  {
                      read.Action("GetStates", "Admin");
                  });
              })
        )
        @Html.ValidationMessageFor(model => model.StateId, "", new { @class = "text-danger" })
    </div>

Upvotes: 0

Views: 1254

Answers (1)

user3559349
user3559349

Reputation:

Use the strongly typed helper so you bind to the property

@(Html.Kendo().DropDownListFor(m => m.StateId)
  .HtmlAttributes(new { style = "width: 250px" })
  .DataTextField("Name")
  .DataValueField("Id")
  .....

Note the .Name() option is not required

Upvotes: 1

Related Questions