Kendo DropDownListFor()与ASP.NET-MVC

问题描述:

我在 ASP.NET-MVC帮助器中遇到问题 我有一张表格,其中将控制器发生的POST创建为操作**创建,并传递了 occurrence 类型的参数,该参数与 Model 表单视图的/strong>,要注册该事件,需要一个 TypeOccurrenceID ,我正在尝试获取该值使用 Html.DropDownListFor(),但这在发布表单时不起作用,参数中的 Occurrence 过去没有 与在DropDownList中选择的OccurrenceType对应的OccurrenceTypeId

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?

这是我的控制器操作

    [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);
    }

这是我的视图

@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")
}

对不起,英语不好!

问题是下拉列表的名称,它必须与要绑定的模型的属性名称相同.

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

示例:

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

替代:

使用DropDownListFor时,实际上不需要name属性.因此,只需删除此行也可以:

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

 .Name("occurrencetype")