剑道 DropDownListFor() 与 ASP.NET-MVC [英] Kendo DropDownListFor() with ASP.NET-MVC

查看:15
本文介绍了剑道 DropDownListFor() 与 ASP.NET-MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET-MVC Helper 中有问题我有一个表单,它使控制器 Occurrence 的 POST 生效 **create 传递一个 occurrence 类型的参数,该参数对应于 Model<插入表单视图的/strong>,为了注册需要TypeOccurrenceID,我正在尝试获取此值使用 Html.DropDownListFor(),但这在表单发布时不起作用,参数中的 Occurrence 过去没有DropDownList中选择的OccurrenceType对应的OccurrenceTypeId

有人遇到同样的问题吗?

这是我的控制器操作

 [HttpPost]公共 ActionResult 创建(发生次数){如果(模型状态.IsValid){尝试{db.Add(发生);返回新的 HttpStatusCodeResult(200);}捕获(异常){返回新的 HttpStatusCodeResult(400);}}返回新的 HttpStatusCodeResult(400);}

这是我的观点

@using Common.Util@using Common.Util.Configuration@使用咖啡馆数据@model 出现<div class="box-form">@using (Ajax.BeginForm("Create", "Occurrence",新的 Ajax 选项{OnSuccess = "OnSuccess()",OnFailure = "OnFailure()"})){@Html.AntiForgeryToken()@Html.ValidationSummary(true)@*区域*@<div class="row-fluid details-field">@(Html.Kendo().DropDownList().Name("区域").HtmlAttributes(new { style = "width:300px" }).OptionLabel("选择区域...").DataTextField("描述").DataValueField("IdArea").DataSource(源=>{source.Read(读=>{read.Action("readAreasForDropDown", "Area");});}))@*事件类型*@@(Html.Kendo().DropDownListFor(m => m.OccurrenceTypeId).Name("发生类型").HtmlAttributes(new { style = "width:300px" }).OptionLabel("选择一个事件类型...").DataTextField("描述").DataValueField("OccurrenceTypeId").DataSource(源=>{source.Read(读=>{read.Action("lerOccurrenceTypeForDropDown",OccurrenceType").数据(filterArea").类型(HttpVerbs.Post);}).ServerFiltering(true);}).启用(假).自动绑定(假).CascadeFrom("区域"))<脚本>函数过滤器区域(){返回 {id: $("#areas").val()};}<button class="k-button">保存</button>}

@section 脚本 {@Scripts.Render("~/bundles/jqueryval")}

抱歉英语不好!

解决方案

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

示例:

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

替代方案:

名称属性在使用 DropDownListFor 时实际上不是必需的.因此,只需删除此行也可以:

 .Name("occurrencetype")

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!

解决方案

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

这篇关于剑道 DropDownListFor() 与 ASP.NET-MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆