Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo DropDownListFor() with ASP.NET-MVC

i have a problem into the ASP.NET-MVC Helper I have a form that give a POST into action **create of the controller Occurrence passing a parameter of type occurrence that corresponding at the Model of the view where the form is inserted, for register the occurrence is needed an TypeOccurrenceID, i'm trying to get this value using Html.DropDownListFor(), but this not working when the form is posted, the Occurrence past in the parameter don't have the OccurrenceTypeId corresponding with the OccurrenceType selected in the DropDownList

Someone had the same problem?

This is my Controller action

    [HttpPost]
    public ActionResult Create(Occurrence occurrence)
    {
        if (ModelState.IsValid)
        {
            try
            {
                db.Add<Occurrence>(occurrence);
                return new HttpStatusCodeResult(200);
            }
            catch (Exception)
            {
                return new HttpStatusCodeResult(400);
            }
        }
        return new HttpStatusCodeResult(400);
    }

Here is my View

@using Common.Util
@using Common.Util.Configuration
@using CafData
@model Occurrence

<div class="box-form">
    @using (Ajax.BeginForm("Create", "Occurrence",
        new AjaxOptions
        {
            OnSuccess = "OnSuccess()",
            OnFailure = "OnFailure()"
        }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

@*Area*@

        <div class="row-fluid details-field">
            @(Html.Kendo().DropDownList()
              .Name("areas")
              .HtmlAttributes(new { style = "width:300px" })
              .OptionLabel("Selecione uma area...")
              .DataTextField("Description")
              .DataValueField("IdArea")
              .DataSource(source =>
              {
                  source.Read(read =>
                  {
                      read.Action("readAreasForDropDown", "Area");
                  });
              })
        )


@*Occurrence type*@

          @(Html.Kendo().DropDownListFor(m => m.OccurrenceTypeId)
              .Name("occurrencetype")
              .HtmlAttributes(new { style = "width:300px" })
              .OptionLabel("Select a occurrence type...")
              .DataTextField("Description")
              .DataValueField("OccurrenceTypeId")
              .DataSource(source =>
              {
                  source.Read(read =>
                  {
                      read.Action("lerOccurrenceTypeForDropDown",                       
                      "OccurrenceType").Data("filterArea"). 
                      Type(HttpVerbs.Post);
                  })
                  .ServerFiltering(true);
              })
              .Enable(false)
              .AutoBind(false)
              .CascadeFrom("areas")
        )

        <script>
            function filterArea() {
                return {
                      id: $("#areas").val()
                 };
            }
        </script>

        <button class="k-button">Save</button>

    }

</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Sorry for the bad english!

like image 621
Lucas Konrath Avatar asked May 29 '13 11:05

Lucas Konrath


Video Answer


1 Answers

The problem was the name of the dropdownlist, it must be the same name as the property of the model that you want bind.

Example:

 @(Html.Kendo().DropDownListFor(m => m.OccurrenceTypeId)
          .Name("OccurrenceTypeId")

Alternative:

The name property is not actually necessary when using DropDownListFor. So just removing this line would work as well:

 .Name("occurrencetype")
like image 93
Lucas Konrath Avatar answered Sep 20 '22 08:09

Lucas Konrath