使用ASP.NET-MVC的Kendo DropDownListFor()

Luc*_*ath 6 kendo-ui kendo-asp.net-mvc

我有到一个问题ASP.NET-MVC助手我有得到POST入动作的形式**创建控制器的发生传递类型的参数发生对应于模型的的视图,其中形式被插入时,用于注册该事件需要一个TypeOccurrenceID,我正在尝试使用Html.DropDownListFor()来获取此值,但是当发布表单时这不起作用,参数中的Occurrence过去没有选择与OccurrenceType对应的OccurrenceTypeId在DropDownList中

有人有同样的问题吗?

这是我的Controller动作

    [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);
    }
Run Code Online (Sandbox Code Playgroud)

这是我的观点

@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")
}
Run Code Online (Sandbox Code Playgroud)

抱歉英文不好!

Luc*_*ath 18

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

例:

 @(Html.Kendo().DropDownListFor(m => m.OccurrenceTypeId)
          .Name("OccurrenceTypeId")
Run Code Online (Sandbox Code Playgroud)

替代方案:

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

 .Name("occurrencetype")
Run Code Online (Sandbox Code Playgroud)

  • 我不相信你在使用`DropDownListFor <>`时需要指定`Name` (5认同)
  • 虽然您不需要为DropDownListFor <>指定名称,但如果要使用CSS选择器使用脚本访问它,则可能需要为其指定名称或ID. (4认同)