简体   繁体   English

mvc3组合框默认选择的值

[英]mvc3 combobox default selected value

I'm using this approach using viewmodel for manipulating values inside combo boxes. 我正在使用这种使用viewmodel的方法来操纵组合框内的值。 Now, I'm struggle to select as default value actual selected value used in create action, not the first one from combo. 现在,我很难选择create动作中使用的实际选定值作为默认值,而不是组合中的第一个。 I know this is fourth parameter in SelectList but I do not know how to use actual UserGroupId cause it's give me an error when using like these 我知道这是SelectList第四个参数,但我不知道如何使用实际的UserGroupId因为这样使用时会给我一个错误

var model = new UserViewModel 
    { 
        UserGroups = new SelectList(GetAllUsers(), "UserId", "Name", UserGroupId) 
    } 
    return View(model); 

 public class UserViewModel 
    { 
        public int UserGroupId { get; set; } 
        public IEnumerable<SelectListItem> UserGroups { get; set; } 
    } 

Like this: 像这样:

var model = new UserViewModel 
{ 
    // preselct an item in the dropdown whose value equals 5
    // This means that inside your `GetAllUsers()` collection you must
    // have an element with UserId=5 and this element will automatically be
    // preselcted. Here you could put any value of course
    UserGroupId = 5,
    UserGroups = new SelectList(GetAllUsers(), "UserId", "Name") 
};
return View(model);

and in your view: 并且在您看来:

@model UserViewModel
...
@Html.DropDownListFor(x => x.UserGroupId, Model.UserGroups)

As for the 4th parameter, it needs to be an actual instance that is part of the collection returned by GetAllUsers() 至于第四个参数,它必须是一个实际实例,该实例是GetAllUsers()返回的集合的一部分。

For example: 例如:

var users = GetAllUsers();
var defaultUser = users[0];
var model = new UserViewModel 
{ 
     UserGroups = new SelectList(users, "UserId", "Name", defaultUser) 
};

return View(model);

You may want to manually build your SelectList rather than using the SelectList constructor. 您可能需要手动构建SelectList,而不是使用SelectList构造函数。 That will give you the control you need over handling the "UserId" and the "Name". 这将为您提供处理“ UserId”和“ Name”所需的控制权。 This also addresses the issue of how to add an integer record id to the select list. 这也解决了如何将整数记录ID添加到选择列表的问题。

Lets start with a method that builds and returns a List<SelectListItem> 让我们从构建并返回List<SelectListItem>的方法开始

public static List<SelectListItem> UserGroups(int userGroupId, int selectedUserId)
{
    var userGroupsQuery = from u in dbContext.Users
                          where u.UserGroupId == userGroupId
                          select u;

    var userSelectList= new List<SelectListItem>();

    foreach (var user in userGroupsQuery.Users)
    {
        userSelectList.Add(
            new SelectListItem
            {
                Text = userGroup.Name,
                Value = userGroup.UserId.ToString(CultureInfo.InvariantCulture),
                Selected = (user.UserId == selectedUserId)
            };
    }

    return userSelectList;
}

Now we have a select list that we can use so lets use it in our action method. 现在我们有了一个可以使用的选择列表,因此让我们在操作方法中使用它。

int selectedUserId = 1000;
int userGroupId = 5;

var userViewModel = new UserViewModel
{
    UserGroups = ListLookup.UserGroups(userGroupId, selectedUserId)
}

return View(userViewModel);

And then in the view. 然后在视图中。

@Html.DropDownList("UserId", Model.Users)

The key here is that the Value property in the select list must be a string, not an integer. 这里的关键是选择列表中的Value属性必须是字符串,而不是整数。 Also note that you cannot call to string inside a lambda expression by default (hence the use of a foreach loop). 另请注意,默认情况下,您无法在lambda表达式内调用字符串(因此使用了foreach循环)。 Most SelectList examples do not show how to use the Selected property to select a particular value in the SelectList item. 大多数SelectList示例未显示如何使用Selected属性在SelectList项目中选择特定值。

I hope this helps :) 我希望这有帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM