Asp.Net Mvc Selectpicker(Asp.Net Mvc Selectpicker)

编程入门 行业动态 更新时间:2024-10-22 20:36:42
Asp.Net Mvc Selectpicker(Asp.Net Mvc Selectpicker)

我正在尝试使用Bootstrap SelectPicker来实现DropDownList Multi Select,以在我的应用程序中选择Schools。

我对MVC和JQuery不是很熟悉,因为我一直使用webforms很久,所以我正在从互联网上学习来完成。

这是一个场景:

在我的布局中,有一个DropDownList:

@*DropDownList Select School*@ @Html.DropDownList("Schools", null, null, new { id = "MultiSelect", @class = "selectpicker form-control", multiple = "", title = "School" })

填充DropDownList的代码:

public ActionResult Class() { IEnumerable<SelectListItem> ListClasses = db.scasy_class .OrderBy(a => a.class_name) .Select(c => new SelectListItem { Value = c.id.ToString(), Text = c.class_name, Selected = false }); ViewBag.Class = ListClasses.ToList(); ClassViewModel classViewModel = new ClassViewModel() { SelectOptions = ListClasses }; return View(classViewModel); }

在布局上,当用户选择一些学校时,使用下拉列表,

$('#MultiSelect').on('change', function () { $.each($("#MultiSelect option"), function () { $.post("/Setup/Student/SetSchool/", { school: $(this).val(), selected: $(this).prop('selected') }); }); });

和控制器;

public ActionResult SetSchool(int school, bool selected) { ArrayList school_nos = Session["Schools"] as ArrayList; if (selected) { if (!school_nos.Contains(school)) { school_nos.Add(school); } } else { if (school_nos.Contains(school)) { school_nos.Remove(school); } } Session["Schools"] = school_nos; return new HttpStatusCodeResult(HttpStatusCode.OK); }

它正在按预期工作,直到这里。 对于下一次重新加载,我尝试使用相同的数据填充下拉列表,但使用会话值显示先前选中的学校使用会话值,因为我将在其他许多页面上使用此信息。

$(document).ready(function () { $.getJSON("/Setup/Student/GetSchool/", function (data) { var myData = []; $.each(data, function (index, item) { if (item.Selected == true) { myData.push(item.Value); } }); //alert(myData); $('#MultiSelect').selectpicker('val', myData); }); });

和控制器;

public JsonResult GetSchool() { IEnumerable<SelectListItem> ListSchools = db.scasy_school .OrderBy(a => a.name) .Select(a => new SelectListItem { Value = a.id.ToString(), Text = a.name}); ArrayList school_nos = Session["Schools"] as ArrayList; List<SelectListItem> ListSchoolsUpdated = new List<SelectListItem>(); foreach (var item in ListSchools) { SelectListItem selListItem; if (school_nos.Contains(item.Value.ToString())) { selListItem = new SelectListItem() { Value = item.Value.ToString(), Text = item.Text, Selected = true }; } else { selListItem = new SelectListItem() { Value = item.Value.ToString(), Text = item.Text, Selected = false }; } ListSchoolsUpdated.Add(selListItem); } return Json(ListSchoolsUpdated, JsonRequestBehavior.AllowGet); }

代码不会引发错误,但我无法显示所选项目的下拉列表。

I am trying to implement a DropDownList Multi Select using Bootstrap Selectpicker for selecting Schools in my application.

I am not very familiar with MVC and JQuery since i have been using webforms for a long time, so i am learning from internet to accomplish.

Here is the scenario:

on my layout, there is a DropDownList:

@*DropDownList Select School*@ @Html.DropDownList("Schools", null, null, new { id = "MultiSelect", @class = "selectpicker form-control", multiple = "", title = "School" })

The code to fill the DropDownList:

public ActionResult Class() { IEnumerable<SelectListItem> ListClasses = db.scasy_class .OrderBy(a => a.class_name) .Select(c => new SelectListItem { Value = c.id.ToString(), Text = c.class_name, Selected = false }); ViewBag.Class = ListClasses.ToList(); ClassViewModel classViewModel = new ClassViewModel() { SelectOptions = ListClasses }; return View(classViewModel); }

On layout, when the user selects some schools, using the dropdownlist,

$('#MultiSelect').on('change', function () { $.each($("#MultiSelect option"), function () { $.post("/Setup/Student/SetSchool/", { school: $(this).val(), selected: $(this).prop('selected') }); }); });

and the controller;

public ActionResult SetSchool(int school, bool selected) { ArrayList school_nos = Session["Schools"] as ArrayList; if (selected) { if (!school_nos.Contains(school)) { school_nos.Add(school); } } else { if (school_nos.Contains(school)) { school_nos.Remove(school); } } Session["Schools"] = school_nos; return new HttpStatusCodeResult(HttpStatusCode.OK); }

it is working as it is expected until here. For the next reload, i am trying to fill the dropdownlist with the same data but show previously selected schools with tick, using Session values, since i will need this information on many other pages.

$(document).ready(function () { $.getJSON("/Setup/Student/GetSchool/", function (data) { var myData = []; $.each(data, function (index, item) { if (item.Selected == true) { myData.push(item.Value); } }); //alert(myData); $('#MultiSelect').selectpicker('val', myData); }); });

and the controller;

public JsonResult GetSchool() { IEnumerable<SelectListItem> ListSchools = db.scasy_school .OrderBy(a => a.name) .Select(a => new SelectListItem { Value = a.id.ToString(), Text = a.name}); ArrayList school_nos = Session["Schools"] as ArrayList; List<SelectListItem> ListSchoolsUpdated = new List<SelectListItem>(); foreach (var item in ListSchools) { SelectListItem selListItem; if (school_nos.Contains(item.Value.ToString())) { selListItem = new SelectListItem() { Value = item.Value.ToString(), Text = item.Text, Selected = true }; } else { selListItem = new SelectListItem() { Value = item.Value.ToString(), Text = item.Text, Selected = false }; } ListSchoolsUpdated.Add(selListItem); } return Json(ListSchoolsUpdated, JsonRequestBehavior.AllowGet); }

Code throws no error, but i cannot have the dropdownlist with selected items shown.

最满意答案

为你的页面声明一个模型类

public class ClassViewModel { public List<int> SelectedSchools {get; set;} public List<SelectListItem> Schools {get; set;} }

在控制器操作中设置选定的学校。

设置您的视图模型

@model ClassViewModel

然后在您的视图中使用此代码显示下拉列表

@Html.ListBoxFor(m=> m.SelectedSchools , Model.Schools, new {id = "MultiSelect", @class = "selectpicker form-control", title = "School" })

Declare a model class for your page

public class ClassViewModel { public List<int> SelectedSchools {get; set;} public List<SelectListItem> Schools {get; set;} }

Set Selected Schools in your controller action.

Set Your View Model

@model ClassViewModel

Then in your view use this code to show dropdownlist

@Html.ListBoxFor(m=> m.SelectedSchools , Model.Schools, new {id = "MultiSelect", @class = "selectpicker form-control", title = "School" })

更多推荐

本文发布于:2023-07-24 20:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Net   Asp   Selectpicker   Mvc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!