IEnumerable模型中的ListBoxFor

时间:2014-12-11 22:15:00

标签: c# asp.net-mvc

我的模特 -

...
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="answerTypesId" id="answerTypesId" 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模型填充列表框下方的表格。此列表框位于表格之外,没有理由在其中。

编辑 - 发布整个视图。

1 个答案:

答案 0 :(得分:0)

  

我试图将视图中的模型视为单个对象,   实际上它是类型的   IEnumerable的。

你的假设是正确的。

  

如何从我的模型中获取_SelectedCountries?

您需要一个新模型并在其中添加IEnumerable。

public class SomeViewModel
{
    public IEnumerable<vw_SpecialQuestionDefinition> 
        SpecialQuestionDefinitions { get; set; }

    public string[] _SelectedCountries { get; set; }
}

然后foreach终于会喜欢这个 -

@foreach (var item in Model.SpecialQuestionDefinitions)