【问题标题】:Kendo DropDownListFor() with ASP.NET-MVC剑道 DropDownListFor() 与 ASP.NET-MVC
【发布时间】:2013-05-24 15:58:15
【问题描述】:

我在 ASP.NET-MVC 帮助程序中遇到问题 我有一个表单,可以将 POST 付诸行动 **create 控制器 Occurrence 传递 occurrence 类型的参数,该参数对应于 Model 的 view 插入 form 的位置,用于注册出现需要 TypeOccurrenceID,我正在尝试获取此值使用 Html.DropDownListFor(),但是在发布表单时这不起作用,参数中的 Occurrence 过去没有 DropDownList中选中的OccurrenceType对应的OccurrenceTypeId

有人遇到过同样的问题吗?

这是我的控制器操作

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

抱歉英语不好!

【问题讨论】:

  • 解决了,但是因为信誉问题,我只能在8小时后发布答案
  • 请更新答案

标签: kendo-ui kendo-asp.net-mvc


【解决方案1】:

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

示例:

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

替代方案:

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

 .Name("occurrencetype")

【讨论】:

  • 我认为您在使用DropDownListFor&lt;&gt;时不需要指定Name
  • 你说得对,真正的问题是 DropDownListFor 中指定的属性的名称不同,如果我取出 .Name 可能会起作用。
  • 我希望我能给你投票一百次。你刚刚为我节省了几个小时的工作时间。
  • 虽然您不需要为 DropDownListFor 工作指定名称,但如果您想使用 CSS 选择器通过脚本访问它,则可能需要为其分配名称或 ID。跨度>
  • @LucasKonrath 非常感谢
猜你喜欢
  • 1970-01-01
  • 2017-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 2015-12-31
相关资源
最近更新 更多