Kendo ui Kendo DropDownListFor()与ASP.NET-MVC
我在ASP.NET-MVC帮助程序中遇到问题 我有一个表单,它将一篇文章发布到动作**创建控制器事件的中,传递一个事件类型的参数,该参数对应于插入表单的视图的模型,为了注册事件,需要一个类型发生ID,我正在尝试使用Html.DropDownListFor()获取此值,但在发布表单时,此操作不起作用,参数中的出现时间过去没有 与DropDownList中选择的OccurrenceType对应的OccurrenceTypeId 有人也有同样的问题 这是我的控制器操作Kendo ui Kendo DropDownListFor()与ASP.NET-MVC,kendo-ui,kendo-asp.net-mvc,Kendo Ui,Kendo Asp.net Mvc,我在ASP.NET-MVC帮助程序中遇到问题 我有一个表单,它将一篇文章发布到动作**创建控制器事件的中,传递一个事件类型的参数,该参数对应于插入表单的视图的模型,为了注册事件,需要一个类型发生ID,我正在尝试使用Html.DropDownListFor()获取此值,但在发布表单时,此操作不起作用,参数中的出现时间过去没有 与DropDownList中选择的OccurrenceType对应的OccurrenceTypeId 有人也有同样的问题 这是我的控制器操作 [HttpPost]
[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);
}
[HttpPost]
公共行动结果创建(事件)
{
if(ModelState.IsValid)
{
尝试
{
db.Add(事件);
返回新的HttpStatusCodeResult(200);
}
捕获(例外)
{
返回新的HttpStatusCodeResult(400);
}
}
返回新的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")
}
@使用Common.Util
@使用Common.Util.Configuration
@使用CafData
@模型发生
@使用(Ajax.BeginForm(“创建”、“发生”),
新选择
{
OnSuccess=“OnSuccess()”,
OnFailure=“OnFailure()”
}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@*区域*@
@(Html.Kendo().DropDownList())
.名称(“区域”)
.HtmlAttributes(新的{style=“width:300px”})
.OptionLabel(“Selecione uma区域…”)
.DataTextField(“说明”)
.DataValueField(“IdArea”)
.DataSource(source=>
{
source.Read(Read=>
{
read.Action(“readAreasForDropDown”、“Area”);
});
})
)
@*发生类型*@
@(Html.Kendo().DropDownListFor(m=>m.OccurrenceTypeId)
.名称(“发生类型”)
.HtmlAttributes(新的{style=“width:300px”})
.OptionLabel(“选择事件类型…”)
.DataTextField(“说明”)
.DataValueField(“发生类型ID”)
.DataSource(source=>
{
source.Read(Read=>
{
read.Action(“lerOccurrenceTypeForDropDown”,
“发生类型”)。数据(“过滤区域”)。
类型(HttpVerbs.Post);
})
.ServerFiltering(true);
})
.Enable(false)
.AutoBind(假)
.来自(“区域”)
)
函数过滤器区域(){
返回{
id:$(“#区域”).val()
};
}
拯救
}
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
对不起,英语不好 问题在于dropdownlist的名称,它必须与要绑定的模型属性的名称相同 示例:
@(Html.Kendo().DropDownListFor(m => m.OccurrenceTypeId)
.Name("OccurrenceTypeId")
备选方案:
@(Html.Kendo().DropDownListFor(m => m.OccurrenceTypeId)
.Name("OccurrenceTypeId")
使用DropDownListFor时,实际上不需要name属性。因此,只要删除这一行也可以:
.Name("occurrencetype")
已解决,但由于声誉问题,我只能在8小时后发布答案。请更新答案。我认为使用
DropDownListFor
时,您不需要指定名称。您是对的,真正的问题是DropDownListFor中指定的属性名称不同,如果我去掉了这个名字,可能会有用。我希望我能给你投上一百票。您刚刚为我节省了几个小时的工作时间。虽然您不需要为DropDownList for指定名称,但如果您想使用CSS选择器通过脚本访问它,您可能需要为它指定一个名称或id。@LucasKonrath非常感谢您