ListBoxFor within IEnumerable model
本文关键字:model IEnumerable within ListBoxFor | 更新日期: 2023-09-27 18:34:04
我的模型 -
...
public string[] _SelectedCountries { get; set; }
而我的观点——
@using System.Collections.Concurrent
@using Example.Models
@model IEnumerable<Example.Models.vw_SpecialQuestionDefinition>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Create", "SpecialQuestionDefinition", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Scripts.Render("~/Scripts/SpecialQuestions/Index.js")
<div class="row">
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Host Country</h4>
</div>
<div class="panel-body">
@*@Html.ListBoxFor("countries", null, new { @class = "sqQuestions" })*@
@Html.ListBoxFor(model => model._SelectedCountries,
new MultiSelectList((List<SelectListItem>)ViewData["countries"], "Value", "Text"),
new { style = "display:block;", @class = "sqQuestions" })
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-5 col-md-12">
<input type="submit" value="Configure" class="btn btn-default"/>
</div>
</div>
<input type="hidden" name="specialQuestionsId" id="specialQuestionsId" value="-1" />
<input type="hidden" name="answerswerTypesId" id="answerswerTypesId" value="-1" />
<input type="hidden" name="hostCountrysId" id="hostCountrysId" value="-1" />
<input type="hidden" name="nationalitysId" id="nationalitysId" value="-1" />
<input type="hidden" name="scopeTypesId" id="scopeTypesId" value="-1" />
}
<h4>Special Question Definition</h4>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Question)
</th>
<th>
@Html.DisplayNameFor(model => model.AnswerType)
</th>
<th>
@Html.DisplayNameFor(model => model.LookupTable)
</th>
<th>
@Html.DisplayNameFor(model => model.CountryName)
</th>
<th>
@Html.DisplayNameFor(model => model.NationalityName)
</th>
<th>
@Html.DisplayNameFor(model => model.ScopeType)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Question)
</td>
<td>
@Html.DisplayFor(modelItem => item.AnswerType)
</td>
<td>
@Html.DisplayFor(modelItem => item.LookupTable)
</td>
<td>
@Html.DisplayFor(modelItem => item.CountryName)
</td>
<td>
@Html.DisplayFor(modelItem => item.NationalityName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ScopeType)
</td>
</tr>
}
</table>
在我的控制器中 -
List<SelectListItem> listCountriesSelectListItems = new List<SelectListItem>();
listSpecialQuestionsSelectListItems.Add(new SelectListItem() { Text = "All", Value = "-1" });
foreach (Country co in db.Countries.OrderBy(c => c.CountryName))
{
SelectListItem selectList = new SelectListItem()
{
Text = co.CountryName,
Value = co.CountryId.ToString()
};
listCountriesSelectListItems.Add(selectList);
}
ViewBag.countries = listCountriesSelectListItems;
所以当我运行应用程序时,我的ListBoxFor
收到此错误 -
'System.Collections.Generic.IEnumerable<Example.Models.vw_SpecialQuestionDefinition>' does not contain a definition for '_SelectedCountries' and no extension method '_SelectedCountries' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Example.Models.vw_SpecialQuestionDefinition>' could be found (are you missing a using directive or an assembly reference?)
我相信我明白为什么我会犯这个错误。因为我试图将视图中的模型视为单个对象,而实际上它是 IEnumerable<Example.Models.vw_SpecialQuestionDefinition>
型。
那我如何从我的模型中获取 _ SelectedCountries
呢?
此外,我使用 IEnumerable 模型填充列表框下方的表。此列表框位于表外部,没有理由位于表内。
编辑 - 发布整个视图。
我试图将视图中的模型视为单个对象, 当实际上它是类型 IE无数。
你的假设是正确的。
那么我如何从我的模型中获取_SelectedCountries呢?
您需要一个新模型并在其中添加 IEnumerable。
public class SomeViewModel
{
public IEnumerable<vw_SpecialQuestionDefinition>
SpecialQuestionDefinitions { get; set; }
public string[] _SelectedCountries { get; set; }
}
然后福拉奇最终会像这样——
@foreach (var item in Model.SpecialQuestionDefinitions)