我遵循了Rick Anderson的示例,但无法使其正常工作。我有原始的多选框,但没有收获选择的多选框。

视图:

@Html.ListBox("Clearances", ViewBag.Clearanceslist as MultiSelectList, new { htmlAttributes = new { @class = "form-control chosen-select", style="width:350px;" } })


在视图下是我的javascript:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
    $('.datepicker').datepicker();
    $(".chosen-select").chosen();
</script>
}


这是我包含在BundleConfig.cs中的脚本:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-{version}.js",
                "~/Scripts/chosen.jquery.js"
                ));

    bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.validate*"));

    // Use the development version of Modernizr to develop with and learn from. Then, when you're
    // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
    bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                "~/Scripts/modernizr-*"));

    bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
              "~/Scripts/bootstrap.js",
              "~/Scripts/bootstrap-datepicker.js",
              "~/Scripts/respond.js"));

    bundles.Add(new StyleBundle("~/Content/css").Include(
              "~/Content/bootstrap.css",
              "~/Content/datepicker.css",
              "~/Content/chosen.css",
              "~/Content/site.css"));
}


控制器:

private MultiSelectList GetClearances(string[] selectedValues)
{
    return new MultiSelectList(db.Clearances.Where(c => c.Active == true), "ClearanceID", "ClearanceName", selectedValues);
}

// GET: CardKeys/Create
public ActionResult Create()
{
    ViewBag.Clearanceslist = GetClearances(null);
    return View();
}


我还想念什么?

最佳答案

您已在包配置中正确地将文件添加到了包中,但是实际上您在哪里调用该包?

尝试如下进行更改

 @section Scripts {
 @Scripts.Render("~/bundles/jquery")
 @Scripts.Render("~/bundles/jqueryval")

 <script type="text/javascript">
$('.datepicker').datepicker();
$(".chosen-select").chosen();
</script>
}

关于javascript - MVC Harvest选择的多选下拉列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33769420/

10-12 14:23