Edit() [Razor]View 的下拉菜单,预加载模型中的数据
我的 Create() 视图中的下拉菜单工作完美。
但是在 Edit() 视图中,我无法将 Create() 期间提交的数据显示在 DropDowns 中,并且在 Create() 上输入的值
我目前只有文本框,并且真的很想拥有数据以下拉列表形式表示,以便于选择。
这是一个示例:
Create() 视图 - 一个下拉列表适用于 EmployeeTypes,并将所选内容存储到 EmployeeTypeId
现在如何才能将其显示在 Edit() 视图中作为相同的下拉列表,但使用EmployeeId 的值已选择?
我有一个用于 Create() 视图的 EmployeeViewModel
但我只是将模型直接传递到 Edit() 视图中
我应该为 Edit() 视图创建某种 Employee“部分类”吗?处理 IEnumerable 列表?
并设置:
var employeeTypes = context.EmployeeTypes.Select(et => new SelectListItem
{
Value = et.EmployeeTypeId.ToString(),
Text = et.Type.ToString()
});
或者我应该将它们作为 ViewData 传递?
如果是这样,如何将列表作为 ViewData 传入,并使其显示为 @Html.DropDownList,并将从 @Model 传入的值作为默认值?
I have the dropdowns in my Create() View working perfect.
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()
I just have textboxs in place at the moment And would really like to have Data Represented in a dropdown for easy selection.
Here is one example:
Create() View - One dropdown is for EmployeeTypes, and stores selected to EmployeeTypeId
Now How do I get that to show up in the Edit() View as the same dropdown, but with Value of EmployeeId already selected?
I have a EmployeeViewModel for the Create() View
But I am just passing the model directly into the Edit() View
Should I create some kind of Employee "partial class" for the Edit() View? to handle the IEnumerable Lists?
and set:
var employeeTypes = context.EmployeeTypes.Select(et => new SelectListItem
{
Value = et.EmployeeTypeId.ToString(),
Text = et.Type.ToString()
});
Or should I pass them in as ViewData?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码中可能存在错误 - 我还没有测试过 - 但你基本上想做的是:
然后在你看来你可以做
There may be bugs in this code - I haven't tested it - but what you basically want to do is:
Then in your view you can do
我最终以这种方式进行了暗示,而且效果就像做梦一样。
控制器代码:
查看代码:
I ended up implimenting this way, and it worked like a dream.
Controller Code:
View Code: