如何使用 ASP.NET MVC 编辑多选列表?

发布于 2024-07-23 11:42:49 字数 377 浏览 6 评论 0原文

我想编辑一个如下所示的对象。 我希望用 UsersGrossList 中的一个或多个用户填充 UsersSelectedList。

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

任何观点的例子都值得赞赏。

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

I'd like to edit an object like the one below. I'd like the UsersSelectedList populated with one or more Users from the UsersGrossList.

Using the standard edit-views in mvc, I get only strings and booleans (not shown below) mapped.
Many of the examples I find on google utilizes early releases of the mvc framework whereas I use the official 1.0 release.

Any examples of the view is appreciated.

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

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

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

发布评论

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

评论(3

筱果果 2024-07-30 11:42:51

将 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);
    }

Use Html.ListBox in combination with IEnumerable SelectListItem

View

         <% using (Html.BeginForm("Category", "Home",
      null,
      FormMethod.Post))
       { %>  
        <%= Html.ListBox("CategoriesSelected",Model.CategoryList )%>

        <input type="submit" value="submit" name="subform" />
        <% }%>

Controller/Model:

        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);
    }
下雨或天晴 2024-07-30 11:42:51

@eu-ge-ne < 非常感谢您的回答 - 在寻找一种方法来从模型中多重选择值列表和模型中多重选择值列表时遇到了很大的麻烦。
使用您的代码,我在编辑/更新页面中使用了 ListBoxFor Html 控件,并在保存时将整个模型传递回我的控制器(包括多个选定值)。

<%= 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) 
} 

) %>

@ eu-ge-ne < thankyou so much for your answer - was having real trouble finding a way to multiselect a list of values from and to model.
Using your code I used the ListBoxFor Html control in my Edit/Update page and passed the whole model back to my controller (including the mulisple selected values) on Save.

<%= 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) 
} 

) %>

苏大泽ㄣ 2024-07-30 11:42:50

假设用户模型具有 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 ) %>

Assuming that User model has Id and Name properties:

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

Or with View Model

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

Controller:

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)
        }
    }

View:

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