8

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!

Lucas Konrath
  • 567
  • 2
  • 8
  • 19

1 Answers1

21

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")
numaroth
  • 1,295
  • 4
  • 25
  • 36
Lucas Konrath
  • 567
  • 2
  • 8
  • 19
  • 6
    I don't believe you need to specify the `Name` when using `DropDownListFor<>` – Matt Millican Sep 27 '13 at 03:12
  • 1
    you're right, the real problem was the Name different of the property specified in the DropDownListFor<>, if i had taken out the .Name probably would work. – Lucas Konrath Sep 27 '13 at 17:40
  • I wish I could up-vote you a hundred times. You just saved me hours of work. – Winks Oct 17 '13 at 06:58
  • 4
    Although you don't need to specify the Name for the DropDownListFor<> to work, you may need to assign it a name or id if you want to access it with scripts using CSS selectors. – B Sharp Jan 04 '14 at 00:18
  • @LucasKonrath Thank you alot – Abdulsalam Elsharif Feb 08 '17 at 15:52
  • @LucasKonrath you are right in saying the name must match with property it is bound to but not having a name is not an option if you have to do further action using the foreign key bound to dropdown. I wish I had stumbled on this post earlier. Thanks – hima Sep 11 '17 at 14:44