asp mvc下拉列表默认值不绑定

时间:2014-08-11 11:08:15

标签: asp.net-mvc-4

控制器中的

我:

 ViewBag.ActivityTypeID = new SelectList(activityTypes, "ActivityTypeID","Name"
    ,workFlowDetail.ActivityTypeID);

并在视野中:

 @Html.DropDownListFor(model => model.ActivityTypeID,new SelectList(ViewBag.ActivityTypeID,"Value","Text"), String.Empty)    
 @Html.ValidationMessageFor(model => model.ActivityTypeID)

我的编辑视图中的下拉列表未选择默认值(workFlowDetail.ActivityTypeID)

我该怎么做? 感谢

2 个答案:

答案 0 :(得分:1)

此问题通常发生在MVC中,因为model.ActivityTypeIDViewBag.ActivityTypeID共有ActivityTypeID这就是问题发生的原因,只需更改Viewbag名称,问题就会得到解决。

这将有效: -

控制器:

ViewBag.ActivityType = new SelectList(activityTypes, "ActivityTypeID","Name"
,workFlowDetail.ActivityTypeID);

查看:

@Html.DropDownListFor(model => model.ActivityTypeID,new SelectList(ViewBag.ActivityType,"Value","Text"), String.Empty)    
@Html.ValidationMessageFor(model => model.ActivityTypeID)

答案 1 :(得分:0)

只需在视图中使用以下内容:

  

@ Html.DropDownList(" ActivityTypeID",ViewBag.ActivityTypeID as SelectList)   @ Html.ValidationMessageFor(型号=> model.ActivityTypeID)