Edit() [Razor]View 的下拉菜单,预加载模型中的数据

发布于 2024-10-24 13:10:38 字数 829 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2024-10-31 13:10:39

这段代码中可能存在错误 - 我还没有测试过 - 但你基本上想做的是:

var etId = ???  // EmployeeTypeId from your model
var employeeTypes = context.EmployeeTypes.Select(et => new SelectListItem
        {
            Value = et.EmployeeTypeId.ToString(),
            Text = et.Type.ToString(),
            Selected = et.EmployeeTypeId == etId
        });
ViewData["EmployeeTypeList"] = employeeTypes.ToList();

然后在你看来你可以做

@Html.DropDownList("EmployeeType", ViewData["EmployeeTypeList"])

There may be bugs in this code - I haven't tested it - but what you basically want to do is:

var etId = ???  // EmployeeTypeId from your model
var employeeTypes = context.EmployeeTypes.Select(et => new SelectListItem
        {
            Value = et.EmployeeTypeId.ToString(),
            Text = et.Type.ToString(),
            Selected = et.EmployeeTypeId == etId
        });
ViewData["EmployeeTypeList"] = employeeTypes.ToList();

Then in your view you can do

@Html.DropDownList("EmployeeType", ViewData["EmployeeTypeList"])
梓梦 2024-10-31 13:10:38

我最终以这种方式进行了暗示,而且效果就像做梦一样。

控制器代码:

 SelectList typelist = new SelectList(context.CompanyType.ToList(), "CompanyTypeId", "Type", context.CompanyType);
            ViewData["CompanyTypes"] = typelist;

查看代码:

@Html.DropDownList("CompanyTypeId", (IEnumerable<SelectListItem>) ViewData["CompanyTypes"])

I ended up implimenting this way, and it worked like a dream.

Controller Code:

 SelectList typelist = new SelectList(context.CompanyType.ToList(), "CompanyTypeId", "Type", context.CompanyType);
            ViewData["CompanyTypes"] = typelist;

View Code:

@Html.DropDownList("CompanyTypeId", (IEnumerable<SelectListItem>) ViewData["CompanyTypes"])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文