我在 ASP.NET-MVC Helper中遇到了问题 我有一个表单,可以操作POST **创建控制器出现,传递出现类型的参数,该参数对应于模型<插入表单的视图的/ strong>,对于注册,需要 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")
}
抱歉英文不好!
答案 0 :(得分:18)
问题是下拉列表的名称,它必须与您想要绑定的模型的属性相同。
示例:强>
@(Html.Kendo().DropDownListFor(m => m.OccurrenceTypeId)
.Name("OccurrenceTypeId")
<强>替代:强>
使用DropDownListFor时,实际上不需要name属性。所以只需删除这一行就可以了:
.Name("occurrencetype")