尝试为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; }
}
答案 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>
}