下拉编辑()[剃刀]查看,pre-装有数据从型号 [英] DropDown for Edit() [Razor]View, Pre-Loaded with Data from Model
问题描述
我在我的Create()查看下拉式菜单中的工作完美。
I have the dropdowns in my Create() View working perfect.
但在编辑()查看我不能得到这是在submited数据的Create()的下拉菜单显示了在创建(enterened值)
But in the Edit() View I can't get the Data that was submited during the Create() to show up in DropDowns with the Value enterened upon Create()
我只是此刻到位textboxs而且真的很想有方便选择下拉数据重新presented。
I just have textboxs in place at the moment And would really like to have Data Represented in a dropdown for easy selection.
这里有一个例子:
创建()查看 - 一个下拉菜单是EmployeeTypes,并选择了EmployeeTypeId商店
Create() View - One dropdown is for EmployeeTypes, and stores selected to EmployeeTypeId
现在我如何得到这在Edit()查看该下拉菜单显示出来,但与雇员的价值已被选中?
Now How do I get that to show up in the Edit() View as the same dropdown, but with Value of EmployeeId already selected?
我对创建()查看一个EmployeeViewModel
I have a EmployeeViewModel for the Create() View
但我只是直接传递模型到编辑()查看
But I am just passing the model directly into the Edit() View
我应该创建某种雇员部分类为编辑()查看?处理IEnumerable的名单?
Should I create some kind of Employee "partial class" for the Edit() View? to handle the IEnumerable Lists?
和设置:
var employeeTypes = context.EmployeeTypes.Select(et => new SelectListItem
{
Value = et.EmployeeTypeId.ToString(),
Text = et.Type.ToString()
});
或者我应该通过他们作为ViewData的?
Or should I pass them in as ViewData?
如果这样你该怎么办作为ViewData的传递一个列表,并把它显示为@ Html.DropDownList与价值从@Model传递作为defualt价值?
If so how to do you pass a List in as ViewData and get it to display as an @Html.DropDownList with the Value passed in from the @Model as the defualt value?
推荐答案
我结束了implimenting这种方式,和它的工作就像一个梦。
I ended up implimenting this way, and it worked like a dream.
控制器code:
SelectList typelist = new SelectList(context.CompanyType.ToList(), "CompanyTypeId", "Type", context.CompanyType);
ViewData["CompanyTypes"] = typelist;
查看code:
@Html.DropDownList("CompanyTypeId", (IEnumerable<SelectListItem>) ViewData["CompanyTypes"])
这篇关于下拉编辑()[剃刀]查看,pre-装有数据从型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!