简体   繁体   English

ASP.NET MVC 多选下拉菜单

[英]ASP.NET MVC multiple select dropdown

I am using the following code to let user select multiple locations on the form.我正在使用以下代码让用户在表单上选择多个位置。

@Html.DropDownListFor(m => m.location_code, Model.location_type, new { @class = "form-control", @multiple = "multiple" }).

location_code is an List<int> and location_type is List<SelectListItem> populated with data. location_code 是List<int> ,location_type 是List<SelectListItem>填充数据。

The code does return me the selected values in the controller, but when the user clicks on edit button the object passed does not show the selected values but instead shows the normal initialized dropdown with nothing selected.该代码确实将控制器中的选定值返回给我,但是当用户单击编辑按钮时,传递的对象不会显示选定的值,而是显示正常的初始化下拉列表,没有任何选择。

What i actually want is that once the user submits the form (including the multiple selected values) it goes to a page where user confirms if the details are correct.If not he presses edit button and the object is again passed to controller.In this phase it should show the multiple values selected.Other fields behave properly .我真正想要的是,一旦用户提交表单(包括多个选择的值),它就会进入一个页面,用户确认详细信息是否正确。如果不是,他按下编辑按钮,对象再次传递给控制器​​。在这个阶段它应该显示选择的多个值。其他字段表现正常。

Any insight on this ?对此有任何见解吗?

In your view:在您看来:

@Html.ListBoxFor(m => m.location_code, Model.location_type)

That's all you need.这就是你所需要的。 You're using a ListBox control so it's already a multiple select list.您正在使用 ListBox 控件,因此它已经是一个多选列表。

Then back in your controller you can get the selected items like this:然后回到你的控制器,你可以得到这样的选定项目:

[HttpPost]
public string SaveResults(List<int> location_code)
{

    if (location_code!= null)
    {
        return string.Join(",", location_code);
    }
    else
    {
        return "No values are selected";
    }
}

I am assuming you already know the general MVC stuff, so with that in mind:我假设你已经知道一般的 MVC 东西,所以请记住:

First get this library and set it up: https://select2.org/getting-started/basic-usage首先获取这个库并设置它: https ://select2.org/getting-started/basic-usage

In your view you can use this在您看来,您可以使用它

@Html.DropDownListFor(model => model.DistrictId, new SelectList(new List<object>(), "Id ", "Description"), "Select District", htmlAttributes: new
                                {
                                    @class = "js-example-placeholder-multiple col-sm-12 form-select form-control",
                                    @multiple="multiple",
                                    id = "DistrictDropDownList"
                                })

You can then add a property viewmodel for the model.DistrictId used in the control above as a List of string as follows然后,您可以为上面控件中使用的 model.DistrictId 添加属性 viewmodel 作为字符串列表,如下所示

public class Places
{
   //Other ViewModel properties
    public List<string> DistrictId {get;set;}
   
}

When you submit your ViewModel back to the controller, you will have the multiple selected Ids as a list of strings.当您将 ViewModel 提交回控制器时,您将拥有多个选定的 Id 作为字符串列表。

MultiSelect DropdownList Using jQuery in Asp.Net MVC and C#.Net :在 Asp.Net MVC 和 C#.Net 中使用 jQuery 的 MultiSelect DropdownList:

first we will create a new mvc application and add the a model class file in model folder.首先,我们将创建一个新的 mvc 应用程序并在模型文件夹中添加一个模型类文件。 In this model class file add the below code.在此模型类文件中添加以下代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
    public class CountryModel
    {
        public List<Country> CountryList { get; set; }
        public string SelectedCountryId { get; set; }
    }
    public class Country
    {
        public string CountryName { get; set; }
    }
}

Now add a controller class file and add the below code.现在添加一个控制器类文件并添加以下代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            CountryModel objcountrymodel = new CountryModel();
            objcountrymodel.CountryList = new List<Country>();
            objcountrymodel.CountryList = CountryDate();
            return View(objcountrymodel);
        }
        public List<Country> CountryDate()
        {
            List<Country> objcountry = new List<Country>();
            objcountry.Add(new Country { CountryName = "America" });
            objcountry.Add(new Country { CountryName = "India" });
            objcountry.Add(new Country { CountryName = "Sri Lanka" });
            objcountry.Add(new Country { CountryName = "China" });
            objcountry.Add(new Country { CountryName = "Pakistan" });
            return objcountry;
        }
    }
}

In above code I have created a collection of country.在上面的代码中,我创建了一个国家的集合。 This list we will bind with the dropdown list.这个列表我们将与下拉列表绑定。 Now create the view and add the below code into the view.现在创建视图并将以下代码添加到视图中。

@model MvcApplication1.Models.CountryModel          
@{
    ViewBag.Title = "MultiSelect DropdownList Using jQuery in Asp.Net MVC and C#.Net";
}
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="../../Scripts/jquery.sumoselect.js" type="text/javascript"></script>
<link href="../../Css/dropdownliststyle.css" rel="stylesheet" type="text/css" />
 <script type="text/javascript">
     $(document).ready(function () {
         window.asd = $('.ddlMultiSlectBox).SumoSelect({ csvDispCount: 4 });
     });
    </script>
Country:
@Html.DropDownListFor(m => m.SelectedCountryId, new SelectList(Model.CountryList, "CountryName", "CountryName"), new {@multiple="multiple",@placeholder="Please select country", @class="ddlMultiSlectBox" })

Here in this code I have added the css and jQuery library reference and used sumoselect function to show multi select dropdown list.在这段代码中,我添加了 css 和 jQuery 库参考,并使用 sumoselect 函数来显示多选下拉列表。

Now we have done run the application to check the output.现在我们已经运行了应用程序来检查输出。

在此处输入图像描述

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

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