C# MVC模型。任何System.ArgumentNullException

C# MVC模型。任何System.ArgumentNullException,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,如主题a中所述,在视图中遇到System.ArgumentNullException: @if (!Model.Any()) { <h1 class="text-center">Nie ma żadnej storny!</h1> } 查看: @model IEnumerable<CmsShop.Models.ViewModels.Pages.PageVM> @{ ViewBag.Title = "Strony"; } <h

如主题a中所述,在视图中遇到System.ArgumentNullException:

@if (!Model.Any())
{
    <h1 class="text-center">Nie ma żadnej storny!</h1>
}
查看:

    @model IEnumerable<CmsShop.Models.ViewModels.Pages.PageVM>

@{
    ViewBag.Title = "Strony";
}

<h2>Strony</h2>

<p>
    @Html.ActionLink("Dodaj Nową Stronę", "AddPage")
</p>

@if (!Model.Any())
{
    <h1 class="text-center">Nie ma żadnej storny!</h1>
}
else
{
    <table class="table">
        <tr>
            <th>
                Tytuł Strony
            </th>
            <th>
                Adres Strony
            </th>
            <th>
                Pasek Boczny
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Slug)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.HasSideBar)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edycja", new { id=item.Id }) |
                    @Html.ActionLink("Details", "Szczegóły", new { id=item.Id }) |
                    @Html.ActionLink("Delete", "Usuń", new { id=item.Id })
                </td>
            </tr>
        }

    </table>
}
    using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using CmsShop.Models.Data;
using CmsShop.Models.ViewModels.Pages;

namespace CmsShop.Controllers
{
    public class PagesController : Controller
    {
        // GET: Admin/Pages
        public ActionResult Index()
        {
            // Deklaracja listy PageViewModel
            List<PageVM> pagesList;


            using (DB db = new DB()) //po wykonaniu wszystkie co jest w usingu, wyczyści swoje zasoby (dobra praktyka)
            {//inicjalizacja listy danych z bazy danych
                pagesList = db.Pages.ToArray().OrderBy(x => x.Sorting).Select(x => new PageVM(x)).ToList(); //automatyczne mapowanie z konstruktora argumentowego w PageVM
            }

            //zwracamy strony do widoku
            return View(pagesList);
        }

    }
}
    using System.ComponentModel.DataAnnotations;
using CmsShop.Models.Data;

namespace CmsShop.Models.ViewModels.Pages
{
    public class PageVM
    {

        public PageVM()
        {

        }

        public PageVM(PageDTO row) //konstruktor przyjmujący wiersz bazy danych
        {
            Id = row.Id;
            Title = row.Title;
            Slug = row.Slug;
            Body = row.Body;
            Sorting = row.Sorting;
            HasSideBar = row.HasSideBar;
        }
        public int Id { get; set; }

        [Required]
        [StringLength(50,MinimumLength = 3)]
        [Display(Name = "Tytuł Strony")]
        public string Title { get; set; }

        [Display(Name = "Adres Strony")]
        public string Slug { get; set; }

        [Required]
        [StringLength(int.MaxValue, MinimumLength = 3)]
        [Display(Name = "Zawartość Strony")]
        public string Body { get; set; }
        public int Sorting { get; set; }
        [Display(Name = "Pasek Boczny")]
        public bool HasSideBar { get; set; }
    }
}

您面临的问题来自return语句中提供给视图的null,原因是
pagesList
未正确初始化。 最有可能的情况是,数据库中没有数据,或者由于任何原因无法连接到数据库,并且要传递的列表保持为空

初始化变量总是一个很好的做法

            // Deklaracja listy PageViewModel
            List<PageVM> pagesList = new List<PageVM>();
//Deklaracja listy PageViewModel
列表页面列表=新列表();
这将解决您的空引用问题。但是,最有可能的情况是,您将不会显示任何内容,但这是由于数据库中缺少数据或连接不良等原因造成的(我们无法看到在db()中执行的操作)

也可以在调用
db.Pages.ToArray()
中使用
ToArray()
。在这方面没有意义,如果它是演讲的一部分,也许考虑另一个。
ToList()
也是不需要的,因为您的模型类型为
IEnumerable

可能这样更容易理解问题 完全错误:

CS1061: 'PageVM' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'PageVM' could be found (are you missing a using directive or an assembly reference?)



M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml(15,13): error CS1061: 'PageVM' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'PageVM' could be found (are you missing a using directive or an assembly reference?)
M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml(35,31): error CS1579: foreach statement cannot operate on variables of type 'PageVM' because 'PageVM' does not contain a public instance definition for 'GetEnumerator'
M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml(47,62): error CS0828: Cannot assign 'method group' to anonymous type property
M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml(48,68): error CS0828: Cannot assign 'method group' to anonymous type property
M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml(49,62): error CS0828: Cannot assign 'method group' to anonymous type property


Wiersz 1:    #pragma checksum "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "A311074B35D42046F94A4AA53D453318D0661EDF"
Wiersz 2:    //------------------------------------------------------------------------------
Wiersz 3:    // <auto-generated>
Wiersz 4:    //     Ten kod został wygenerowany przez narzędzie.
Wiersz 5:    //     Wersja wykonawcza:4.0.30319.42000
Wiersz 6:    //
Wiersz 7:    //     Zmiany w tym pliku mogą spowodować nieprawidłowe zachowanie i zostaną utracone, jeśli
Wiersz 8:    //     kod zostanie ponownie wygenerowany.
Wiersz 9:    // </auto-generated>
Wiersz 10:   //------------------------------------------------------------------------------
Wiersz 11:   
Wiersz 12:   namespace ASP {
Wiersz 13:       using System;
Wiersz 14:       using System.Collections.Generic;
Wiersz 15:       using System.IO;
Wiersz 16:       using System.Linq;
Wiersz 17:       using System.Net;
Wiersz 18:       using System.Web;
Wiersz 19:       using System.Web.Helpers;
Wiersz 20:       using System.Web.Security;
Wiersz 21:       using System.Web.UI;
Wiersz 22:       using System.Web.WebPages;
Wiersz 23:       using System.Web.Mvc;
Wiersz 24:       using System.Web.Mvc.Ajax;
Wiersz 25:       using System.Web.Mvc.Html;
Wiersz 26:       using System.Web.Optimization;
Wiersz 27:       using System.Web.Routing;
Wiersz 28:       using CmsShop;
Wiersz 29:       
Wiersz 30:       
Wiersz 31:       public class _Page_Views_Pages_Index_cshtml : System.Web.Mvc.WebViewPage<CmsShop.Models.ViewModels.Pages.PageVM> {
Wiersz 32:           
Wiersz 33:   #line hidden
Wiersz 34:           
Wiersz 35:           public _Page_Views_Pages_Index_cshtml() {
Wiersz 36:           }
Wiersz 37:           
Wiersz 38:           protected ASP.global_asax ApplicationInstance {
Wiersz 39:               get {
Wiersz 40:                   return ((ASP.global_asax)(Context.ApplicationInstance));
Wiersz 41:               }
Wiersz 42:           }
Wiersz 43:           
Wiersz 44:           public override void Execute() {
Wiersz 45:               
Wiersz 46:               #line 3 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 47:     
Wiersz 48:       ViewBag.Title = "Strony";
Wiersz 49:   
Wiersz 50:               
Wiersz 51:               #line default
Wiersz 52:               #line hidden
Wiersz 53:   BeginContext("~/Views/Pages/Index.cshtml", 85, 36, true);
Wiersz 54:   
Wiersz 55:   WriteLiteral("\r\n\r\n<h2>Strony</h2>\r\n\r\nModel.\r\n<p>\r\n");
Wiersz 56:   
Wiersz 57:   EndContext("~/Views/Pages/Index.cshtml", 85, 36, true);
Wiersz 58:   
Wiersz 59:   BeginContext("~/Views/Pages/Index.cshtml", 121, 4, true);
Wiersz 60:   
Wiersz 61:   WriteLiteral("    ");
Wiersz 62:   
Wiersz 63:   EndContext("~/Views/Pages/Index.cshtml", 121, 4, true);
Wiersz 64:   
Wiersz 65:   BeginContext("~/Views/Pages/Index.cshtml", 126, 47, false);
Wiersz 66:   
Wiersz 67:               
Wiersz 68:               #line 11 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 69:   Write(Html.ActionLink("Dodaj Nową Stronę", "AddPage"));
Wiersz 70:   
Wiersz 71:               
Wiersz 72:               #line default
Wiersz 73:               #line hidden
Wiersz 74:   EndContext("~/Views/Pages/Index.cshtml", 126, 47, false);
Wiersz 75:   
Wiersz 76:   BeginContext("~/Views/Pages/Index.cshtml", 173, 12, true);
Wiersz 77:   
Wiersz 78:   WriteLiteral("\r\n</p>\r\n\r\n\r\n");
Wiersz 79:   
Wiersz 80:   EndContext("~/Views/Pages/Index.cshtml", 173, 12, true);
Wiersz 81:   
Wiersz 82:               
Wiersz 83:               #line 15 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 84:    if (!Model.Any())
Wiersz 85:   {
Wiersz 86:   
Wiersz 87:               
Wiersz 88:               #line default
Wiersz 89:               #line hidden
Wiersz 90:   BeginContext("~/Views/Pages/Index.cshtml", 208, 7, true);
Wiersz 91:   
Wiersz 92:   WriteLiteral("    <h1");
Wiersz 93:   
Wiersz 94:   EndContext("~/Views/Pages/Index.cshtml", 208, 7, true);
Wiersz 95:   
Wiersz 96:   BeginContext("~/Views/Pages/Index.cshtml", 215, 20, true);
Wiersz 97:   
Wiersz 98:   WriteLiteral(" class=\"text-center\"");
Wiersz 99:   
Wiersz 100:  EndContext("~/Views/Pages/Index.cshtml", 215, 20, true);
Wiersz 101:  
Wiersz 102:  BeginContext("~/Views/Pages/Index.cshtml", 235, 29, true);
Wiersz 103:  
Wiersz 104:  WriteLiteral(">Nie ma żadnej strony!</h1>\r\n");
Wiersz 105:  
Wiersz 106:  EndContext("~/Views/Pages/Index.cshtml", 235, 29, true);
Wiersz 107:  
Wiersz 108:              
Wiersz 109:              #line 18 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 110:  }
Wiersz 111:  else
Wiersz 112:  {
Wiersz 113:  
Wiersz 114:              
Wiersz 115:              #line default
Wiersz 116:              #line hidden
Wiersz 117:  BeginContext("~/Views/Pages/Index.cshtml", 276, 10, true);
Wiersz 118:  
Wiersz 119:  WriteLiteral("    <table");
Wiersz 120:  
Wiersz 121:  EndContext("~/Views/Pages/Index.cshtml", 276, 10, true);
Wiersz 122:  
Wiersz 123:  BeginContext("~/Views/Pages/Index.cshtml", 286, 14, true);
Wiersz 124:  
Wiersz 125:  WriteLiteral(" class=\"table\"");
Wiersz 126:  
Wiersz 127:  EndContext("~/Views/Pages/Index.cshtml", 286, 14, true);
Wiersz 128:  
Wiersz 129:  BeginContext("~/Views/Pages/Index.cshtml", 300, 258, true);
Wiersz 130:  
Wiersz 131:  WriteLiteral(@">
Wiersz 132:          <tr>
Wiersz 133:              <th>
Wiersz 134:                  Tytuł Strony
Wiersz 135:              </th>
Wiersz 136:              <th>
Wiersz 137:                  Adres Strony
Wiersz 138:              </th>
Wiersz 139:              <th>
Wiersz 140:                  Pasek Boczny
Wiersz 141:              </th>
Wiersz 142:              <th></th>
Wiersz 143:          </tr>
Wiersz 144:  
Wiersz 145:  ");
Wiersz 146:  
Wiersz 147:  EndContext("~/Views/Pages/Index.cshtml", 300, 258, true);
Wiersz 148:  
Wiersz 149:              
Wiersz 150:              #line 35 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 151:          
Wiersz 152:              
Wiersz 153:              #line default
Wiersz 154:              #line hidden
Wiersz 155:              
Wiersz 156:              #line 35 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 157:           foreach (var item in Model) {
Wiersz 158:  
Wiersz 159:              
Wiersz 160:              #line default
Wiersz 161:              #line hidden
Wiersz 162:  BeginContext("~/Views/Pages/Index.cshtml", 598, 40, true);
Wiersz 163:  
Wiersz 164:  WriteLiteral("            <tr>\r\n                <td>\r\n");
Wiersz 165:  
Wiersz 166:  EndContext("~/Views/Pages/Index.cshtml", 598, 40, true);
Wiersz 167:  
Wiersz 168:  BeginContext("~/Views/Pages/Index.cshtml", 638, 20, true);
Wiersz 169:  
Wiersz 170:  WriteLiteral("                    ");
Wiersz 171:  
Wiersz 172:  EndContext("~/Views/Pages/Index.cshtml", 638, 20, true);
Wiersz 173:  
Wiersz 174:  BeginContext("~/Views/Pages/Index.cshtml", 659, 40, false);
Wiersz 175:  
Wiersz 176:              
Wiersz 177:              #line 38 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 178:                 Write(Html.DisplayFor(modelItem => item.Title));
Wiersz 179:  
Wiersz 180:              
Wiersz 181:              #line default
Wiersz 182:              #line hidden
Wiersz 183:  EndContext("~/Views/Pages/Index.cshtml", 659, 40, false);
Wiersz 184:  
Wiersz 185:  BeginContext("~/Views/Pages/Index.cshtml", 699, 47, true);
Wiersz 186:  
Wiersz 187:  WriteLiteral("\r\n                </td>\r\n                <td>\r\n");
Wiersz 188:  
Wiersz 189:  EndContext("~/Views/Pages/Index.cshtml", 699, 47, true);
Wiersz 190:  
Wiersz 191:  BeginContext("~/Views/Pages/Index.cshtml", 746, 20, true);
Wiersz 192:  
Wiersz 193:  WriteLiteral("                    ");
Wiersz 194:  
Wiersz 195:  EndContext("~/Views/Pages/Index.cshtml", 746, 20, true);
Wiersz 196:  
Wiersz 197:  BeginContext("~/Views/Pages/Index.cshtml", 767, 39, false);
Wiersz 198:  
Wiersz 199:              
Wiersz 200:              #line 41 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 201:                 Write(Html.DisplayFor(modelItem => item.Slug));
Wiersz 202:  
Wiersz 203:              
Wiersz 204:              #line default
Wiersz 205:              #line hidden
Wiersz 206:  EndContext("~/Views/Pages/Index.cshtml", 767, 39, false);
Wiersz 207:  
Wiersz 208:  BeginContext("~/Views/Pages/Index.cshtml", 806, 47, true);
Wiersz 209:  
Wiersz 210:  WriteLiteral("\r\n                </td>\r\n                <td>\r\n");
Wiersz 211:  
Wiersz 212:  EndContext("~/Views/Pages/Index.cshtml", 806, 47, true);
Wiersz 213:  
Wiersz 214:  BeginContext("~/Views/Pages/Index.cshtml", 853, 20, true);
Wiersz 215:  
Wiersz 216:  WriteLiteral("                    ");
Wiersz 217:  
Wiersz 218:  EndContext("~/Views/Pages/Index.cshtml", 853, 20, true);
Wiersz 219:  
Wiersz 220:  BeginContext("~/Views/Pages/Index.cshtml", 874, 45, false);
Wiersz 221:  
Wiersz 222:              
Wiersz 223:              #line 44 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 224:                 Write(Html.DisplayFor(modelItem => item.HasSideBar));
Wiersz 225:  
Wiersz 226:              
Wiersz 227:              #line default
Wiersz 228:              #line hidden
Wiersz 229:  EndContext("~/Views/Pages/Index.cshtml", 874, 45, false);
Wiersz 230:  
Wiersz 231:  BeginContext("~/Views/Pages/Index.cshtml", 919, 47, true);
Wiersz 232:  
Wiersz 233:  WriteLiteral("\r\n                </td>\r\n                <td>\r\n");
Wiersz 234:  
Wiersz 235:  EndContext("~/Views/Pages/Index.cshtml", 919, 47, true);
Wiersz 236:  
Wiersz 237:  BeginContext("~/Views/Pages/Index.cshtml", 966, 20, true);
Wiersz 238:  
Wiersz 239:  WriteLiteral("                    ");
Wiersz 240:  
Wiersz 241:  EndContext("~/Views/Pages/Index.cshtml", 966, 20, true);
Wiersz 242:  
Wiersz 243:  BeginContext("~/Views/Pages/Index.cshtml", 987, 53, false);
Wiersz 244:  
Wiersz 245:              
Wiersz 246:              #line 47 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 247:                 Write(Html.ActionLink("Edit", "Edycja", new { id=item.Id }));
Wiersz 248:  
Wiersz 249:              
Wiersz 250:              #line default
Wiersz 251:              #line hidden
Wiersz 252:  EndContext("~/Views/Pages/Index.cshtml", 987, 53, false);
Wiersz 253:  
Wiersz 254:  BeginContext("~/Views/Pages/Index.cshtml", 1040, 4, true);
Wiersz 255:  
Wiersz 256:  WriteLiteral(" |\r\n");
Wiersz 257:  
Wiersz 258:  EndContext("~/Views/Pages/Index.cshtml", 1040, 4, true);
Wiersz 259:  
Wiersz 260:  BeginContext("~/Views/Pages/Index.cshtml", 1044, 20, true);
Wiersz 261:  
Wiersz 262:  WriteLiteral("                    ");
Wiersz 263:  
Wiersz 264:  EndContext("~/Views/Pages/Index.cshtml", 1044, 20, true);
Wiersz 265:  
Wiersz 266:  BeginContext("~/Views/Pages/Index.cshtml", 1065, 59, false);
Wiersz 267:  
Wiersz 268:              
Wiersz 269:              #line 48 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 270:                 Write(Html.ActionLink("Details", "Szczegóły", new { id=item.Id }));
Wiersz 271:  
Wiersz 272:              
Wiersz 273:              #line default
Wiersz 274:              #line hidden
Wiersz 275:  EndContext("~/Views/Pages/Index.cshtml", 1065, 59, false);
Wiersz 276:  
Wiersz 277:  BeginContext("~/Views/Pages/Index.cshtml", 1124, 4, true);
Wiersz 278:  
Wiersz 279:  WriteLiteral(" |\r\n");
Wiersz 280:  
Wiersz 281:  EndContext("~/Views/Pages/Index.cshtml", 1124, 4, true);
Wiersz 282:  
Wiersz 283:  BeginContext("~/Views/Pages/Index.cshtml", 1128, 20, true);
Wiersz 284:  
Wiersz 285:  WriteLiteral("                    ");
Wiersz 286:  
Wiersz 287:  EndContext("~/Views/Pages/Index.cshtml", 1128, 20, true);
Wiersz 288:  
Wiersz 289:  BeginContext("~/Views/Pages/Index.cshtml", 1149, 53, false);
Wiersz 290:  
Wiersz 291:              
Wiersz 292:              #line 49 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 293:                 Write(Html.ActionLink("Delete", "Usuń", new { id=item.Id }));
Wiersz 294:  
Wiersz 295:              
Wiersz 296:              #line default
Wiersz 297:              #line hidden
Wiersz 298:  EndContext("~/Views/Pages/Index.cshtml", 1149, 53, false);
Wiersz 299:  
Wiersz 300:  BeginContext("~/Views/Pages/Index.cshtml", 1202, 44, true);
Wiersz 301:  
Wiersz 302:  WriteLiteral("\r\n                </td>\r\n            </tr>\r\n");
Wiersz 303:  
Wiersz 304:  EndContext("~/Views/Pages/Index.cshtml", 1202, 44, true);
Wiersz 305:  
Wiersz 306:              
Wiersz 307:              #line 52 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 308:          }
Wiersz 309:  
Wiersz 310:              
Wiersz 311:              #line default
Wiersz 312:              #line hidden
Wiersz 313:  BeginContext("~/Views/Pages/Index.cshtml", 1257, 16, true);
Wiersz 314:  
Wiersz 315:  WriteLiteral("\r\n    </table>\r\n");
Wiersz 316:  
Wiersz 317:  EndContext("~/Views/Pages/Index.cshtml", 1257, 16, true);
Wiersz 318:  
Wiersz 319:              
Wiersz 320:              #line 55 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 321:  }
Wiersz 322:  
Wiersz 323:              
Wiersz 324:              #line default
Wiersz 325:              #line hidden
Wiersz 326:  BeginContext("~/Views/Pages/Index.cshtml", 1276, 4, true);
Wiersz 327:  
Wiersz 328:  WriteLiteral("\r\n\r\n");
Wiersz 329:  
Wiersz 330:  EndContext("~/Views/Pages/Index.cshtml", 1276, 4, true);
Wiersz 331:  
Wiersz 332:          }
Wiersz 333:      }
Wiersz 334:  }
Wiersz 335:  

我不知道我是否理解,你是说你的页面总是输入
@if(!Model.Any())
if吗?你想让它显示元素吗?如果没有显示元素,是因为
db.Pages.ToArray()
没有返回任何元素。你试过调试吗?
db.Pages.ToArray()有什么作用?谢谢您的回复。我尝试了debugin,但即使在发送请求时,它也会在Pages param上停止,不会到达Index.ToArray,使用Pages方法将所有对象都放到表中,我可以稍后对其进行排序并将其传输到列表中。如果我理解正确的话。我在没有阵列的情况下尝试了,但结果是一样的。要测试它,请在页面列表中添加一些虚拟数据,不要调用db.Pages。并查看是否可以看到虚拟数据。感谢您的响应,DB:using System.data.Entity;namespace CmsShop.Models.Data{public class DB:DbContext{public DbSet Pages{get;set;}}}}}初始化列表pagesList=new List();不幸的是,他不会帮我的。我在DB中有一条记录,我把它添加到测试中。考虑使用Stask.LINQ;视图顶部的代码>(例如,模型定义上方)。
CS1061: 'PageVM' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'PageVM' could be found (are you missing a using directive or an assembly reference?)



M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml(15,13): error CS1061: 'PageVM' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'PageVM' could be found (are you missing a using directive or an assembly reference?)
M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml(35,31): error CS1579: foreach statement cannot operate on variables of type 'PageVM' because 'PageVM' does not contain a public instance definition for 'GetEnumerator'
M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml(47,62): error CS0828: Cannot assign 'method group' to anonymous type property
M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml(48,68): error CS0828: Cannot assign 'method group' to anonymous type property
M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml(49,62): error CS0828: Cannot assign 'method group' to anonymous type property


Wiersz 1:    #pragma checksum "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "A311074B35D42046F94A4AA53D453318D0661EDF"
Wiersz 2:    //------------------------------------------------------------------------------
Wiersz 3:    // <auto-generated>
Wiersz 4:    //     Ten kod został wygenerowany przez narzędzie.
Wiersz 5:    //     Wersja wykonawcza:4.0.30319.42000
Wiersz 6:    //
Wiersz 7:    //     Zmiany w tym pliku mogą spowodować nieprawidłowe zachowanie i zostaną utracone, jeśli
Wiersz 8:    //     kod zostanie ponownie wygenerowany.
Wiersz 9:    // </auto-generated>
Wiersz 10:   //------------------------------------------------------------------------------
Wiersz 11:   
Wiersz 12:   namespace ASP {
Wiersz 13:       using System;
Wiersz 14:       using System.Collections.Generic;
Wiersz 15:       using System.IO;
Wiersz 16:       using System.Linq;
Wiersz 17:       using System.Net;
Wiersz 18:       using System.Web;
Wiersz 19:       using System.Web.Helpers;
Wiersz 20:       using System.Web.Security;
Wiersz 21:       using System.Web.UI;
Wiersz 22:       using System.Web.WebPages;
Wiersz 23:       using System.Web.Mvc;
Wiersz 24:       using System.Web.Mvc.Ajax;
Wiersz 25:       using System.Web.Mvc.Html;
Wiersz 26:       using System.Web.Optimization;
Wiersz 27:       using System.Web.Routing;
Wiersz 28:       using CmsShop;
Wiersz 29:       
Wiersz 30:       
Wiersz 31:       public class _Page_Views_Pages_Index_cshtml : System.Web.Mvc.WebViewPage<CmsShop.Models.ViewModels.Pages.PageVM> {
Wiersz 32:           
Wiersz 33:   #line hidden
Wiersz 34:           
Wiersz 35:           public _Page_Views_Pages_Index_cshtml() {
Wiersz 36:           }
Wiersz 37:           
Wiersz 38:           protected ASP.global_asax ApplicationInstance {
Wiersz 39:               get {
Wiersz 40:                   return ((ASP.global_asax)(Context.ApplicationInstance));
Wiersz 41:               }
Wiersz 42:           }
Wiersz 43:           
Wiersz 44:           public override void Execute() {
Wiersz 45:               
Wiersz 46:               #line 3 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 47:     
Wiersz 48:       ViewBag.Title = "Strony";
Wiersz 49:   
Wiersz 50:               
Wiersz 51:               #line default
Wiersz 52:               #line hidden
Wiersz 53:   BeginContext("~/Views/Pages/Index.cshtml", 85, 36, true);
Wiersz 54:   
Wiersz 55:   WriteLiteral("\r\n\r\n<h2>Strony</h2>\r\n\r\nModel.\r\n<p>\r\n");
Wiersz 56:   
Wiersz 57:   EndContext("~/Views/Pages/Index.cshtml", 85, 36, true);
Wiersz 58:   
Wiersz 59:   BeginContext("~/Views/Pages/Index.cshtml", 121, 4, true);
Wiersz 60:   
Wiersz 61:   WriteLiteral("    ");
Wiersz 62:   
Wiersz 63:   EndContext("~/Views/Pages/Index.cshtml", 121, 4, true);
Wiersz 64:   
Wiersz 65:   BeginContext("~/Views/Pages/Index.cshtml", 126, 47, false);
Wiersz 66:   
Wiersz 67:               
Wiersz 68:               #line 11 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 69:   Write(Html.ActionLink("Dodaj Nową Stronę", "AddPage"));
Wiersz 70:   
Wiersz 71:               
Wiersz 72:               #line default
Wiersz 73:               #line hidden
Wiersz 74:   EndContext("~/Views/Pages/Index.cshtml", 126, 47, false);
Wiersz 75:   
Wiersz 76:   BeginContext("~/Views/Pages/Index.cshtml", 173, 12, true);
Wiersz 77:   
Wiersz 78:   WriteLiteral("\r\n</p>\r\n\r\n\r\n");
Wiersz 79:   
Wiersz 80:   EndContext("~/Views/Pages/Index.cshtml", 173, 12, true);
Wiersz 81:   
Wiersz 82:               
Wiersz 83:               #line 15 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 84:    if (!Model.Any())
Wiersz 85:   {
Wiersz 86:   
Wiersz 87:               
Wiersz 88:               #line default
Wiersz 89:               #line hidden
Wiersz 90:   BeginContext("~/Views/Pages/Index.cshtml", 208, 7, true);
Wiersz 91:   
Wiersz 92:   WriteLiteral("    <h1");
Wiersz 93:   
Wiersz 94:   EndContext("~/Views/Pages/Index.cshtml", 208, 7, true);
Wiersz 95:   
Wiersz 96:   BeginContext("~/Views/Pages/Index.cshtml", 215, 20, true);
Wiersz 97:   
Wiersz 98:   WriteLiteral(" class=\"text-center\"");
Wiersz 99:   
Wiersz 100:  EndContext("~/Views/Pages/Index.cshtml", 215, 20, true);
Wiersz 101:  
Wiersz 102:  BeginContext("~/Views/Pages/Index.cshtml", 235, 29, true);
Wiersz 103:  
Wiersz 104:  WriteLiteral(">Nie ma żadnej strony!</h1>\r\n");
Wiersz 105:  
Wiersz 106:  EndContext("~/Views/Pages/Index.cshtml", 235, 29, true);
Wiersz 107:  
Wiersz 108:              
Wiersz 109:              #line 18 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 110:  }
Wiersz 111:  else
Wiersz 112:  {
Wiersz 113:  
Wiersz 114:              
Wiersz 115:              #line default
Wiersz 116:              #line hidden
Wiersz 117:  BeginContext("~/Views/Pages/Index.cshtml", 276, 10, true);
Wiersz 118:  
Wiersz 119:  WriteLiteral("    <table");
Wiersz 120:  
Wiersz 121:  EndContext("~/Views/Pages/Index.cshtml", 276, 10, true);
Wiersz 122:  
Wiersz 123:  BeginContext("~/Views/Pages/Index.cshtml", 286, 14, true);
Wiersz 124:  
Wiersz 125:  WriteLiteral(" class=\"table\"");
Wiersz 126:  
Wiersz 127:  EndContext("~/Views/Pages/Index.cshtml", 286, 14, true);
Wiersz 128:  
Wiersz 129:  BeginContext("~/Views/Pages/Index.cshtml", 300, 258, true);
Wiersz 130:  
Wiersz 131:  WriteLiteral(@">
Wiersz 132:          <tr>
Wiersz 133:              <th>
Wiersz 134:                  Tytuł Strony
Wiersz 135:              </th>
Wiersz 136:              <th>
Wiersz 137:                  Adres Strony
Wiersz 138:              </th>
Wiersz 139:              <th>
Wiersz 140:                  Pasek Boczny
Wiersz 141:              </th>
Wiersz 142:              <th></th>
Wiersz 143:          </tr>
Wiersz 144:  
Wiersz 145:  ");
Wiersz 146:  
Wiersz 147:  EndContext("~/Views/Pages/Index.cshtml", 300, 258, true);
Wiersz 148:  
Wiersz 149:              
Wiersz 150:              #line 35 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 151:          
Wiersz 152:              
Wiersz 153:              #line default
Wiersz 154:              #line hidden
Wiersz 155:              
Wiersz 156:              #line 35 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 157:           foreach (var item in Model) {
Wiersz 158:  
Wiersz 159:              
Wiersz 160:              #line default
Wiersz 161:              #line hidden
Wiersz 162:  BeginContext("~/Views/Pages/Index.cshtml", 598, 40, true);
Wiersz 163:  
Wiersz 164:  WriteLiteral("            <tr>\r\n                <td>\r\n");
Wiersz 165:  
Wiersz 166:  EndContext("~/Views/Pages/Index.cshtml", 598, 40, true);
Wiersz 167:  
Wiersz 168:  BeginContext("~/Views/Pages/Index.cshtml", 638, 20, true);
Wiersz 169:  
Wiersz 170:  WriteLiteral("                    ");
Wiersz 171:  
Wiersz 172:  EndContext("~/Views/Pages/Index.cshtml", 638, 20, true);
Wiersz 173:  
Wiersz 174:  BeginContext("~/Views/Pages/Index.cshtml", 659, 40, false);
Wiersz 175:  
Wiersz 176:              
Wiersz 177:              #line 38 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 178:                 Write(Html.DisplayFor(modelItem => item.Title));
Wiersz 179:  
Wiersz 180:              
Wiersz 181:              #line default
Wiersz 182:              #line hidden
Wiersz 183:  EndContext("~/Views/Pages/Index.cshtml", 659, 40, false);
Wiersz 184:  
Wiersz 185:  BeginContext("~/Views/Pages/Index.cshtml", 699, 47, true);
Wiersz 186:  
Wiersz 187:  WriteLiteral("\r\n                </td>\r\n                <td>\r\n");
Wiersz 188:  
Wiersz 189:  EndContext("~/Views/Pages/Index.cshtml", 699, 47, true);
Wiersz 190:  
Wiersz 191:  BeginContext("~/Views/Pages/Index.cshtml", 746, 20, true);
Wiersz 192:  
Wiersz 193:  WriteLiteral("                    ");
Wiersz 194:  
Wiersz 195:  EndContext("~/Views/Pages/Index.cshtml", 746, 20, true);
Wiersz 196:  
Wiersz 197:  BeginContext("~/Views/Pages/Index.cshtml", 767, 39, false);
Wiersz 198:  
Wiersz 199:              
Wiersz 200:              #line 41 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 201:                 Write(Html.DisplayFor(modelItem => item.Slug));
Wiersz 202:  
Wiersz 203:              
Wiersz 204:              #line default
Wiersz 205:              #line hidden
Wiersz 206:  EndContext("~/Views/Pages/Index.cshtml", 767, 39, false);
Wiersz 207:  
Wiersz 208:  BeginContext("~/Views/Pages/Index.cshtml", 806, 47, true);
Wiersz 209:  
Wiersz 210:  WriteLiteral("\r\n                </td>\r\n                <td>\r\n");
Wiersz 211:  
Wiersz 212:  EndContext("~/Views/Pages/Index.cshtml", 806, 47, true);
Wiersz 213:  
Wiersz 214:  BeginContext("~/Views/Pages/Index.cshtml", 853, 20, true);
Wiersz 215:  
Wiersz 216:  WriteLiteral("                    ");
Wiersz 217:  
Wiersz 218:  EndContext("~/Views/Pages/Index.cshtml", 853, 20, true);
Wiersz 219:  
Wiersz 220:  BeginContext("~/Views/Pages/Index.cshtml", 874, 45, false);
Wiersz 221:  
Wiersz 222:              
Wiersz 223:              #line 44 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 224:                 Write(Html.DisplayFor(modelItem => item.HasSideBar));
Wiersz 225:  
Wiersz 226:              
Wiersz 227:              #line default
Wiersz 228:              #line hidden
Wiersz 229:  EndContext("~/Views/Pages/Index.cshtml", 874, 45, false);
Wiersz 230:  
Wiersz 231:  BeginContext("~/Views/Pages/Index.cshtml", 919, 47, true);
Wiersz 232:  
Wiersz 233:  WriteLiteral("\r\n                </td>\r\n                <td>\r\n");
Wiersz 234:  
Wiersz 235:  EndContext("~/Views/Pages/Index.cshtml", 919, 47, true);
Wiersz 236:  
Wiersz 237:  BeginContext("~/Views/Pages/Index.cshtml", 966, 20, true);
Wiersz 238:  
Wiersz 239:  WriteLiteral("                    ");
Wiersz 240:  
Wiersz 241:  EndContext("~/Views/Pages/Index.cshtml", 966, 20, true);
Wiersz 242:  
Wiersz 243:  BeginContext("~/Views/Pages/Index.cshtml", 987, 53, false);
Wiersz 244:  
Wiersz 245:              
Wiersz 246:              #line 47 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 247:                 Write(Html.ActionLink("Edit", "Edycja", new { id=item.Id }));
Wiersz 248:  
Wiersz 249:              
Wiersz 250:              #line default
Wiersz 251:              #line hidden
Wiersz 252:  EndContext("~/Views/Pages/Index.cshtml", 987, 53, false);
Wiersz 253:  
Wiersz 254:  BeginContext("~/Views/Pages/Index.cshtml", 1040, 4, true);
Wiersz 255:  
Wiersz 256:  WriteLiteral(" |\r\n");
Wiersz 257:  
Wiersz 258:  EndContext("~/Views/Pages/Index.cshtml", 1040, 4, true);
Wiersz 259:  
Wiersz 260:  BeginContext("~/Views/Pages/Index.cshtml", 1044, 20, true);
Wiersz 261:  
Wiersz 262:  WriteLiteral("                    ");
Wiersz 263:  
Wiersz 264:  EndContext("~/Views/Pages/Index.cshtml", 1044, 20, true);
Wiersz 265:  
Wiersz 266:  BeginContext("~/Views/Pages/Index.cshtml", 1065, 59, false);
Wiersz 267:  
Wiersz 268:              
Wiersz 269:              #line 48 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 270:                 Write(Html.ActionLink("Details", "Szczegóły", new { id=item.Id }));
Wiersz 271:  
Wiersz 272:              
Wiersz 273:              #line default
Wiersz 274:              #line hidden
Wiersz 275:  EndContext("~/Views/Pages/Index.cshtml", 1065, 59, false);
Wiersz 276:  
Wiersz 277:  BeginContext("~/Views/Pages/Index.cshtml", 1124, 4, true);
Wiersz 278:  
Wiersz 279:  WriteLiteral(" |\r\n");
Wiersz 280:  
Wiersz 281:  EndContext("~/Views/Pages/Index.cshtml", 1124, 4, true);
Wiersz 282:  
Wiersz 283:  BeginContext("~/Views/Pages/Index.cshtml", 1128, 20, true);
Wiersz 284:  
Wiersz 285:  WriteLiteral("                    ");
Wiersz 286:  
Wiersz 287:  EndContext("~/Views/Pages/Index.cshtml", 1128, 20, true);
Wiersz 288:  
Wiersz 289:  BeginContext("~/Views/Pages/Index.cshtml", 1149, 53, false);
Wiersz 290:  
Wiersz 291:              
Wiersz 292:              #line 49 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 293:                 Write(Html.ActionLink("Delete", "Usuń", new { id=item.Id }));
Wiersz 294:  
Wiersz 295:              
Wiersz 296:              #line default
Wiersz 297:              #line hidden
Wiersz 298:  EndContext("~/Views/Pages/Index.cshtml", 1149, 53, false);
Wiersz 299:  
Wiersz 300:  BeginContext("~/Views/Pages/Index.cshtml", 1202, 44, true);
Wiersz 301:  
Wiersz 302:  WriteLiteral("\r\n                </td>\r\n            </tr>\r\n");
Wiersz 303:  
Wiersz 304:  EndContext("~/Views/Pages/Index.cshtml", 1202, 44, true);
Wiersz 305:  
Wiersz 306:              
Wiersz 307:              #line 52 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 308:          }
Wiersz 309:  
Wiersz 310:              
Wiersz 311:              #line default
Wiersz 312:              #line hidden
Wiersz 313:  BeginContext("~/Views/Pages/Index.cshtml", 1257, 16, true);
Wiersz 314:  
Wiersz 315:  WriteLiteral("\r\n    </table>\r\n");
Wiersz 316:  
Wiersz 317:  EndContext("~/Views/Pages/Index.cshtml", 1257, 16, true);
Wiersz 318:  
Wiersz 319:              
Wiersz 320:              #line 55 "M:\Google Drive\Projekty do nauki\CMS_Shop\CmsShop\CmsShop\Views\Pages\Index.cshtml"
Wiersz 321:  }
Wiersz 322:  
Wiersz 323:              
Wiersz 324:              #line default
Wiersz 325:              #line hidden
Wiersz 326:  BeginContext("~/Views/Pages/Index.cshtml", 1276, 4, true);
Wiersz 327:  
Wiersz 328:  WriteLiteral("\r\n\r\n");
Wiersz 329:  
Wiersz 330:  EndContext("~/Views/Pages/Index.cshtml", 1276, 4, true);
Wiersz 331:  
Wiersz 332:          }
Wiersz 333:      }
Wiersz 334:  }
Wiersz 335:  
using System.Data.Entity;

namespace CmsShop.Models.Data
{
    public class DB:DbContext
    {
        public DbSet<PageDTO> Pages { get; set; }
    }
}
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace CmsShop.Models.Data
{
    [Table("tblPages")]
    public class PageDTO
    {
        [Key]
        public int Id { get; set; }
        public string Title { get; set; }
        public string Slug { get; set; }
        public string Body { get; set; }
        public int Sorting { get; set; }
        public bool HasSideBar { get; set; }
    }
}