2016-06-12 21 views
0

嘗試使用選擇插件的MultiSelectList,但我有一個問題,甚至在我的視圖中顯示插件。你們可以弄清楚爲什麼?jquery在Asp.net MVC 4中選擇的插件不顯示在視圖

查看:

@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

想通了自己。檢查了我的網絡選項卡,發現一些文件未加載。您需要將一些代碼替換爲下面的適當部分。

@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> 
} 
+0

我的jquery行不得不包括寬度以及由於某種原因,但這個答案很好,謝謝。 $('。chzn-select')。chosen({width:「95%」}); – adudley

相關問題