Asp.Net Mvc Selectpicker

时间:2017-02-20 05:39:19

标签: c# jquery asp.net-mvc twitter-bootstrap bootstrap-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);
}

它正在运作,直到这里。 对于下一次重新加载,我正在尝试使用相同的数据填充下拉列表,但是使用会话值显示之前选择的带有tick的学校,因为我将在许多其他页面上需要此信息。

$(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);
}

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

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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