8

我在 ASP.NET- MVC帮助程序中遇到问题我有一个表单可以POST付诸实施注册事件需要一个TypeOccurrenceID,我正在尝试使用Html.DropDownListFor()获取此值,但是在发布表单时这不起作用,参数中的Occurrence过去没有与所选 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")
}

对不起英语不好!

4

1 回答 1

21

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

例子:

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

选择:

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

 .Name("occurrencetype")
于 2013-09-26T18:26:44.630 回答