MultiSelect DropdownList 在 Asp.Net MVC 和 C#.Net 中使用 jQuery:
首先我们将创建一个新的 mvc 应用程序并在模型文件夹中添加一个模型类文件。在这个模型类文件中添加以下代码。
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; }
}
}
现在添加一个控制器类文件并添加以下代码。
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;
}
}
}
在上面的代码中,我创建了一个国家/地区的集合。这个列表我们将与下拉列表绑定。现在创建视图并将以下代码添加到视图中。
@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" })
在这段代码中,我添加了 css 和 jQuery 库引用,并使用 sumoselect 函数显示多选下拉列表。
现在我们已经运行了应用程序来检查输出。