我正在努力将数据放入下拉框中。数据不是预定义的数据集,因此它不像将数据硬编码到下拉框中那么简单。数据已创建,一旦创建,我希望它在下拉框中填充。目前,数据已通过复选框显示。我也希望有一个能够调出intellisense的搜索功能。例如,当我输入“ALE”时,我希望它显示带有字母“ALE”的数据,例如“Alex”,“Alexander”,“Alexis”等。请理解我对MVC很新
简而言之,我正在努力摆脱我的计划。我的程序创建“研究人员”,一旦系统上捕获“研究员”,“研究员”现在可以进行研究“提交”。因此,当捕获“提交”时,“研究员”应该能够从下拉框中搜索他/她的名字(以及其他名称,如有必要)。由于会有很多名称,“研究员”也应该在下拉列表旁边有一个搜索功能。
创建提交视图:
<div class="form-group">
@Html.Label("Researchers", new { @class = "control-label col-md-2" })
<div class="col-md-offset-2 col-md-10">
@{
int cnt = 0;
List<RMS.ViewModels.AssignedResearcherData> researchers = ViewBag.Researcher;
foreach (var researcher in researchers)
{
if (cnt++ % 3 == 0)
{
@:</tr><tr>
}
@:<td>
<input type="checkbox"
name="selectedResearchers"
value="@researcher.ResearcherID"
@(Html.Raw(researcher.Assigned ? "checked=\"checked\"" : "")) />
@researcher.FullName
@:</td>
}
@:</tr>
}
</div>
</div>
提交控制器:
private void PopulateAssignedResearcherData(Submission submission)
{
var allResearchers = db.Researcher;
var submissionResearchers = new HashSet<int>(submission.Researcher.Select(i => i.ResearcherID));
var viewModel = new List<AssignedResearcherData>();
foreach (var researcher in allResearchers)
{
viewModel.Add(new AssignedResearcherData
{
ResearcherID = researcher.ResearcherID,
FirstName = researcher.FirstName,
Surname = researcher.Surname,
Assigned = submissionResearchers.Contains(researcher.ResearcherID)
});
}
ViewBag.Researcher = viewModel;
// ViewBag.ResearcherSelect = new MultiSelectList(viewModel, "ResearcherID", "FullName");
}
答案 0 :(得分:1)
您可以使用Select2等第三方插件来实现可搜索的下拉列表 https://select2.github.io/
您只需要生成复选框,生成选择选项,然后调用select2:
//include library at top
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
// init select2
<script type="text/javascript">
$('#myDDl').select2();
</script>
// generate select instead of checkbox
<select id="myDDl" name="selectedResearchers" multiple="multiple">
@foreach (var researcher in researchers)
{
<option value="@researcher.ResearcherID"
@(Html.Raw(researcher.Assigned ? "checked=\"checked\"" : ""))>
@researcher.FullName
</option>
}
</select>
请务必结帐select2以获取您可以传入的更多选项 https://select2.github.io/examples.html
答案 1 :(得分:0)
阅读您的问题我不确定您是否在首先生成下拉框时遇到问题(但是您的第一句话看起来就像是这样)。好吧,如果您正在寻求帮助,那么基本上您需要做的就是将您的viewModel
列表转换为C#中的MultipleSelectList
,然后在Razor中生成下拉列表 -
C#
private void PopulateAssignedResearcherData(Submission submission)
{
var allResearchers = db.Researcher;
var submissionResearchers = new HashSet<int>(submission.Researcher.Select(i => i.ResearcherID));
var viewModel = new List<AssignedResearcherData>();
foreach (var researcher in allResearchers)
{
viewModel.Add(new AssignedResearcherData
{
ResearcherID = researcher.ResearcherID,
FirstName = researcher.FirstName,
Surname = researcher.Surname,
Fullname = researcher.FirstName + " " + researcher.Surname,
Assigned = submissionResearchers.Contains(researcher.ResearcherID)
});
}
ViewBag.Researcher = viewModel;
ViewBag.ResearcherSelect = new MultiSelectList(viewModel, "ResearcherID", "FullName", viewModel.Where(rs => rs.Assigned);
}
剃刀
<div class="form-group">
@Html.Label("Researchers", new { @class = "control-label col-md-2" })
<div class="col-md-offset-2 col-md-10">
@{
int cnt = 0;
List<RMS.ViewModels.AssignedResearcherData> researchers = ViewBag.Researcher;
foreach (var researcher in researchers)
{
if (cnt++ % 3 == 0)
{
@:</tr><tr>
}
@:<td>
@Html.TextBox("researcherSearch")
@Html.DropDownList("researcherList", ViewBag.ResearcherSelect)
@:</td>
}
@:</tr>
}
</div>
</div>
一旦完成,使用其他答案或jQuery UI的自动完成(http://jqueryui.com/autocomplete/)中的建议添加搜索功能应该相对简单。
如果你去了jQuery UI路线,我建议你可以将ViewBag.Researcher
变量转换为JSON以用作数据源。