设置下拉列表中选定的值

发布于 2024-11-09 10:41:49 字数 712 浏览 0 评论 0原文

大家好,我在其中一页上的下拉列表中遇到了奇怪的错误!

我对 3 个下拉列表使用了这样的代码:

//GetTypes returns collection of all availibale types from database 
      ViewData["Types"] = new SelectList(dataManager.IssueTypes.GetTypes(), "Id", "Title", issue.Type);

效果很好!

但是当我这样使用它时:

// GetRoles returns collection of all availibale roles
ViewData["Roles"] = new SelectList(dataManager.Membership.GetRoles(), "RoleId", "RoleName",
                dataManager.Membership.GetUserRole(id));

它总是显示默认值! 我用调试器多次查看这段代码,但一切似乎都很好! 将不胜感激任何形式的帮助!

查看代码:

%: Html.DropDownList("RoleId", ViewData["Roles"] as IEnumerable<SelectListItem>)%>

Hello every one i have strange bug with dropdownlist on one of the pages!

I used code such as this for 3 dropdownlists:

//GetTypes returns collection of all availibale types from database 
      ViewData["Types"] = new SelectList(dataManager.IssueTypes.GetTypes(), "Id", "Title", issue.Type);

and it works great!

But when i used it like this:

// GetRoles returns collection of all availibale roles
ViewData["Roles"] = new SelectList(dataManager.Membership.GetRoles(), "RoleId", "RoleName",
                dataManager.Membership.GetUserRole(id));

it always shows default value!
I looked trough this code with debuger many times but everithing seems fine!
Will be grateful for any kind of help!

View code:

%: Html.DropDownList("RoleId", ViewData["Roles"] as IEnumerable<SelectListItem>)%>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你的呼吸 2024-11-16 10:41:49

使用视图模型而不是 ViewData 怎么样?每当我看到有人使用 ViewData 时,我都觉得自己有义务指出这一点,我个人认为这是最佳实践。

因此,首先定义视图将使用的视图模型:

public class RolesViewModel
{
    public string SelectedRoleId { get; set; }
    public IEnumerable<SelectListItem> Roles { get; set; }
}

现在让我们继续讨论将填充此视图模型并将其传递给视图的控制器操作:

public ActionResult Foo(string id)
{
    var model = new RolesViewModel
    {
        SelectedRoleId = dataManager.Membership.GetUserRole(id),
        Roles = dataManager.Membership.GetRoles().Select(x => new SelectListItem
        {
            Value = x.RoleId,
            Text = x.RoleName
        })
    };
    return View(model);
}

好的,现在您应该确保 SelectedRoleId 表示 Roles 集合中存在的字符串值。这将预选下拉列表。

最后是强类型视图:

<%= Html.DropDownListFor(
    x => x.SelectedRoleId, 
    new SelectList(Model.Roles, "Value", "Text")
)%>

How about using view models instead of ViewData? Everytime I see someone using ViewData I feel myself in the obligation to point this out as personally I consider as best practices.

So start with defining a view model that will be used by your view:

public class RolesViewModel
{
    public string SelectedRoleId { get; set; }
    public IEnumerable<SelectListItem> Roles { get; set; }
}

Now let's move on to the controller action that will populate this view model and pass it to the view:

public ActionResult Foo(string id)
{
    var model = new RolesViewModel
    {
        SelectedRoleId = dataManager.Membership.GetUserRole(id),
        Roles = dataManager.Membership.GetRoles().Select(x => new SelectListItem
        {
            Value = x.RoleId,
            Text = x.RoleName
        })
    };
    return View(model);
}

OK, now you should ensure that SelectedRoleId represents a string value which is present in the Roles collection. This is what will preselect the dropdown list.

And finally the strongly typed view:

<%= Html.DropDownListFor(
    x => x.SelectedRoleId, 
    new SelectList(Model.Roles, "Value", "Text")
)%>
软甜啾 2024-11-16 10:41:49

我不知道你的 dataManager.Membershop.GetUserRole(id) 返回什么类型?

但是您需要更改此调用中的第四个参数:

ViewData["Roles"] = new SelectList(dataManager.Membership.GetRoles(), "RoleId", "RoleName",
                dataManager.Membership.GetUserRole(id));

更改为:

ViewData["Roles"] = new SelectList(dataManager.Membership.GetRoles(), "RoleId", "RoleName",
                dataManager.Membership.GetUserRole(id).RoleId);

注意最后的 RoleId 吗?您指定为所选项目的对象应该是该项目的 Value 属性的值 - 而不是整个对象。

I don't know what type your dataManager.Membershop.GetUserRole(id) returns?

But you need to change the 4th parameter in this call:

ViewData["Roles"] = new SelectList(dataManager.Membership.GetRoles(), "RoleId", "RoleName",
                dataManager.Membership.GetUserRole(id));

To something like:

ViewData["Roles"] = new SelectList(dataManager.Membership.GetRoles(), "RoleId", "RoleName",
                dataManager.Membership.GetUserRole(id).RoleId);

Notice the RoleId at the end? The object you specify as the selected item, should be the value of the item's Value property - not the entire object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文