Ошибка CS1061: «Список <TrainingProductViewModel>» не содержит определения при попытке использовать ModelView - PullRequest
1 голос
/ 12 марта 2020

Я столкнулся с проблемой. Я слежу за учебником по Pluralsight: объединение MVC представлений с использованием одностраничных методов

Я не могу получить доступ к свойствам в классе модели просмотра из index.cs html page

   Compiler Error Message: CS1061: 'List<TrainingProductViewModel>' does not contain a definition for 'Products' and no accessible extension method 'Products' accepting a first argument of type 'List<TrainingProductViewModel>' could be found (are you missing a using directive or an assembly reference?)

Source Error:


Line 38:             </thead>
Line 39:             <tbody>
Line 40:                 @foreach (var item in Model.Products)
Line 41:                 {
Line 42:                     <tr>

Класс ModelView

namespace PTCData
{
    public class TrainingProductViewModel
    {
        public TrainingProductViewModel()
        {
            Products = new List<TrainingProduct>();
            EventCommand = "List";
        }

        public string EventCommand { get; set; }
        public List<TrainingProduct> Products { get; set; }

        public void HandleRequest()
        {
            switch (EventCommand.ToLower()){

                case "list":
                    Get();
                    break;


                default:
                    break;

            }

        }

        private void Get()
        {
            TrainingProductManager mgr = new TrainingProductManager();
            Products = mgr.Get();
        }
    }
}

Страница index.cs html - не удается получить доступ к Model.Products и свойствам

@model List<PTCData.TrainingProductViewModel>
@{
    ViewBag.Title = "Home Page";
}

@using (Html.BeginForm())
{


    <!-- Begin Search Area-->
    <p>&nbsp;</p>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h1 class="panel-title">Search</h1>
        </div>
        <div class="panel-body">
            <div class="form-group">
                @Html.Label("Product Name");
                @Html.TextBox("SearchProductName", "", new { @class = "form-control" })
            </div>
        </div>
        <div class="panel-footer">
            <button id="btnSearch" class="btn btn-sm btn-primary">Search</button>
            <button id="btnReset" class="btn btn-sm btn-primary">Reset</button>
        </div>
    </div>
    <!-- End Search Area-->
    <!-- Begin List Area-->
    <div class="table-responsive">
        <table class="table table-condensed table-bordered table-striped table-hover">
            <thead>
                <tr>
                    <th>Product Name</th>
                    <th>Introduction Date</th>
                    <th>Url</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Products)
                {
                    <tr>
                        <td>@item.ProductName</td>
                        <td>@item.IntroductionDate</td>
                        <td>@item.Url</td>
                        <td>@item.Price.ToString("c")</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <!-- End List Area-->
}
@section scripts{

    <script></script>
    }


1 Ответ

1 голос
/ 12 марта 2020

Ваша модель внутри представления должна быть @model PTCData.TrainingProductViewModel, а не списком.

Список - это коллекция типа, поэтому List<TrainingProductViewModel> не будет содержать Products свойство , но отдельные элементы в списке будут .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...