【问题标题】:How can a multi-select-list be edited using asp.net mvc?如何使用 asp.net mvc 编辑多选列表?
【发布时间】:2010-11-02 23:08:00
【问题描述】:

我想编辑一个像下面这样的对象。我希望从 UsersGrossList 中填充一个或多个用户的 UsersSelectedList。

使用 mvc 中的标准编辑视图,我只得到映射的字符串和布尔值(未在下面显示)。 我在 google 上找到的许多示例都使用了 mvc 框架的早期版本,而我使用的是官方 1.0 版本。

感谢任何视图示例。

public class NewResultsState
{
    public IList<User> UsersGrossList { get; set; }
    public IList<User> UsersSelectedList { get; set; }
}

【问题讨论】:

    标签: c# asp.net-mvc multi-select


    【解决方案1】:

    假设 User 模型具有 Id 和 Name 属性:

    <%= Html.ListBox("users", Model.UsersGrossList.Select(
        x => new SelectListItem {
            Text = x.Name,
            Value = x.Id,
            Selected = Model.UsersSelectedList.Any(y => y.Id == x.Id)
        }
    ) %>
    

    或使用视图模型

    public class ViewModel {
        public Model YourModel;
        public IEnumerable<SelectListItem> Users;
    }
    

    控制器:

    var usersGrossList = ...
    var model = ...
    
    var viewModel = new ViewModel {
        YourModel = model;
        Users = usersGrossList.Select(
            x => new SelectListItem {
                Text = x.Name,
                Value = x.Id,
                Selected = model.UsersSelectedList.Any(y => y.Id == x.Id)
            }
        }
    

    查看:

    <%= Html.ListBox("users", Model.Users ) %>
    

    【讨论】:

      【解决方案2】:

      结合使用 Html.ListBox 和 IEnumerable SelectListItem

      查看

               <% using (Html.BeginForm("Category", "Home",
            null,
            FormMethod.Post))
             { %>  
              <%= Html.ListBox("CategoriesSelected",Model.CategoryList )%>
      
              <input type="submit" value="submit" name="subform" />
              <% }%>
      

      控制器/模型:

              public List<CategoryInfo> GetCategoryList()
          {
              List<CategoryInfo> categories = new List<CategoryInfo>();
              categories.Add( new CategoryInfo{ Name="Beverages", Key="Beverages"});
              categories.Add( new CategoryInfo{ Name="Food", Key="Food"});
              categories.Add(new CategoryInfo { Name = "Food1", Key = "Food1" });
              categories.Add(new CategoryInfo { Name = "Food2", Key = "Food2" });
              return categories;
          }
      
          public class ProductViewModel
          {
              public IEnumerable<SelectListItem> CategoryList { get; set; }
              public IEnumerable<string> CategoriesSelected { get; set; }
      
          }
          public ActionResult Category(ProductViewModel model )
          {
            IEnumerable<SelectListItem> categoryList =
                                      from category in GetCategoryList()
                                      select new SelectListItem
                                      {
                                          Text = category.Name,
                                          Value = category.Key,
                                          Selected = (category.Key.StartsWith("Food"))
                                      };
            model.CategoryList = categoryList;
      
            return View(model);
          }
      

      【讨论】:

        【解决方案3】:

        @ eu-ge-ne

        <%= Html.ListBoxFor(model => model, Model.UsersGrossList.Select( 
        x => new SelectListItem { 
            Text = x.Name, 
            Value = x.Id, 
            Selected = Model.UsersSelectedList.Any(y => y.Id == x.Id) 
        } 
        

        ) %>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-09
          • 1970-01-01
          • 1970-01-01
          • 2019-11-13
          • 2011-05-13
          相关资源
          最近更新 更多