jquery在Asp.net中选择的插件MVC 4没有在视图中显示

时间:2016-06-12 00:45:58

标签: asp.net-mvc-4 jquery-chosen

尝试为MultiSelectList使用所选插件,但我在查看时甚至显示插件时遇到问题。你们能搞清楚为什么吗?

查看:

@model test.Models.Employee
....
<script src="~/Scripts/chosen.jquery.min.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<link href="~/Content/chosen.min.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
    $(document).ready(function () {
        $('.chzn-select').chosen();
    });
</script>

@using (Html.BeginForm())
{
    @Html.LabelFor(model => model.Name)
    @Html.EditorFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name)

    @Html.LabelFor(model => model.Services) 
    @Html.ListBoxFor(model => model.Services, ViewBag.serviceList as MultiSelectList, 
        new { @class = "chzn-select", id="service", data_placeholder = "Choose  Services..." })
    @Html.ValidationMessageFor(model => model.Services)

    <input type="submit" value="Create" class="btn btn-default" />
}

<script>
    $(".chzn-select").chosen();
</script>

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

如果我想在用户提交表单时正确绑定服务字段中的数据,并将其保存到MYSQL表中,是否需要将public string Services { get; set; }的模型修复为public IEnumerable>string> Services { get; set; }? 当我使用IEnumerable并迁移它时,我没有看到Services的列让我担心保存服务数据。

型号:

public class Employee
{
    [Key]
    public string EmpId { get; set; }
    public string Name { get; set; }
    public string Services { get; set; }
}

1 个答案:

答案 0 :(得分:2)

自己想出来。检查我的网络选项卡,发现某些文件未加载。您需要将一些代码替换为适当的部分,如下所示。

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")      
    <script src="@Url.Content("~/Scripts/chosen.jquery.js")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/chosen.min.css")" rel="stylesheet" type="text/css" />

    <script type="text/javascript">
        $(document).ready(function () {
            $('.chzn-select').chosen();
        });
    </script> 
}