查看@foreach

时间:2014-07-24 11:14:34

标签: asp.net-mvc razor

我有这个:

查看:

@using Multitask.Regenboog.Medicijnverstrekking.Domain
@using Multitask.Regenboog.Medicijnverstrekking.WebApplication.ViewModels.FAQ
@model  FaqOverviewModel


@{
    ViewBag.Title = "Index";
}

<h2>Support</h2>

@using (Html.BeginForm("SaveFaq", "FAQ", FormMethod.Post))
{

    @*<table align="right" border="1">
            <tr>hallo</tr>
            <tr>hallo2</tr>

        </table>*@



    <p>
        <span class="fixedLabelWidth control-label col-sm-4 col-md-3 col-xl-2">@Html.LabelFor(model => model.SelectedCategoriedFaqId, "Kies een Categorie:")</span>
        @Html.DropDownListFor(x => x.SelectedCategoriedFaqId, Model.FAQCategorieItems, "Selecteer een Categorie")
        <br /><br />
        <span class="fixedLabelWidth control-label col-sm-4 col-md-3 col-xl-2">@Html.LabelFor(model => model.EmailBericht, "Kies een Onderwerp:")</span>
        @*<span class="fixedLabelWidth "></span><b> een onderwerp</b*@ <select name="Naam" id="FaqCategorie_Id"></select>


    </p>

    <p>
        <span class="fixedLabelWidth control-label col-sm-4 col-md-3 col-xl-2">@Html.LabelFor(model => model.EmailBericht, "Bericht:")</span>
        @Html.TextAreaFor(x => x.EmailBericht)
    </p>

    <p>
        <span class="fixedLabelWidth control-label col-sm-4 col-md-3 col-xl-2">@Html.LabelFor(model => model.AfzenderNaam, "Afzender:")</span>
        @Html.TextBoxFor(x => x.AfzenderNaam)
    </p>

    <p>
        <span class="fixedLabelWidth control-label col-sm-4 col-md-3 col-xl-2">@Html.LabelFor(model => model.TelefoonNr, "Tel:")</span>
        @Html.TextBoxFor(x => x.TelefoonNr)
    </p>


    <p>

    <p>
        @Html.TextBoxFor(x => x.Vraag)

        <input type="submit" value="Zoek" />
    </p>

<p>
    Zoek een vraag: @Html.TextBox("SearchString")
    <input type="submit" value="Search" />
</p>
 }



<table class="table">
    <tr>
        <th>
            @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter })
        </th>
        <th>
            First Name
        </th>
        <th>
            @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter=ViewBag.CurrentFilter })
        </th>
        <th></th>
    </tr>


    @foreach (var item in Model) {
        <tr>

            <td>
                @Html.DisplayFor(modelItem => item.Vraag)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Antwoord)
            </td>

        </tr>
}

</table>
<br />
@*Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))*@

@section scripts
{
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/Form")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/Bundles/Q")
    @Scripts.Render("~/Bundles/moment")
    @Scripts.Render("~/bundles/Utils")



    @Scripts.Render("~/bundles/Jquery-ui")


    <script type="text/javascript">

        function AddToCart(id) {
            $.ajax({
                url: "/Medicijnverstrekking/FAQ/SaveFaq/",
                data: { id: id },
                success: function () {
                    alert('Added');
                }
            });
        }


        $(document).ready(function () {
            $("#SelectedCategoriedFaqId").change(function () {
                $.ajax({
                    url: "/Medicijnverstrekking/FAQ/SubCategorie/",
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        Categorieid: this.value
                    },
                    success: function (data) {
                        //  $('SelectedCategoriedFaqId').val('').remove();
                        //alert("Data retrieval successfull");
                        var items = "";
                        $('#FaqCategorie_Id').empty();
                        $.each(data, function (i, val) {
                            var opt = $("<option></option>")
                              .attr("value", val.Value)
                              .text(val.Text);
                            $('#FaqCategorie_Id').append(opt);
                        });

                    },
                    error: function (xhr) {
                        alert("Something seems Wrong");
                    }
                });

            });

        });

    </script>
}

但是我收到了这个错误:

  

foreach语句不能对类型&#39; Multitask.Regenboog.Medicijnverstrekking.WebApplication.ViewModels.FAQ.FaqOverviewModel&#39;的变量进行操作。因为&#39; Multitask.Regenboog.Medicijnverstrekking.WebApplication.ViewModels.FAQ.FaqOverviewModel&#39;不包含&#39; GetEnumerator&#39;的公开定义c:\ Projects \ Multitask.Regenboog.Medicijnverstrekking-trunk \ WebApplication \ Views \ FAQ \ Index.cshtml 80 6 WebApplication(WebApplication \ WebApplication)

2 个答案:

答案 0 :(得分:1)

你有

@foreach (var item in Model) {
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.Vraag)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Antwoord)
        </td>

    </tr>
}

然而你的FaqOverviewModel ViewModel并不是实现IEnumerable(List等)的东西。

@foreach (var item in Model)应该是@foreach (var item in Model.SomeCollectionOfThings)吗? (其中SomeCollectionOfThings是具有ListVraag属性的对象的Antwoord属性

答案 1 :(得分:0)

试试这个:

@model FaqOverviewModel
...
...
@foreach (var item in IEnumerable<Model>) {