Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a multi-select-list be edited using asp.net mvc?

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; }
}
like image 447
Anders Juul Avatar asked Jun 15 '09 08:06

Anders Juul


People also ask

How to implement multiple select (multiselect) dropdownlist with checkboxes in net?

To implement a Multiple Select (MultiSelect) DropDownList with CheckBoxes in . NET, we will need to make use of checkbox control and apply the jQuery Bootstrap Multi-Select Plugin to it. Provide a Project nam e and confirm or change the Location. Select Create Select the latest version of ASP.NET Core in the drop-down (.

How do I select multiple items in a list in NETnet?

NET MVC, an advanced ListBox control with a corresponding checkbox for each item in a list. Users select or unselect multiple items by checking or unchecking the checkbox without using the shift or ctrl keys. The MultiSelectListBox control also includes filtering and a select all feature.

Do users know how to use a multiselect list?

My guess is that most users don't know how to use a multiselect list and, without some special directions on the page, won't even know that they can make multiple selections. Really, my feeling is that, if you need multiple selections, you should be using checkboxes (which users do know how to use and instantly recognize).

How do I create a drop down list in MVC?

1) First, fire up a project in Visual Studio for ASP.NET MVC web application. 2) Make sure the latest build of EntityFramework is added to the project. 3) Create your primary class that will be used for the parent form that will contain the dropdown list.


2 Answers

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 ) %>
like image 178
eu-ge-ne Avatar answered Sep 28 '22 08:09

eu-ge-ne


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);
    }
like image 45
Mathias F Avatar answered Sep 28 '22 09:09

Mathias F