簡體   English   中英

MVC 3 - 異常詳細信息:System.InvalidOperationException

[英]MVC 3 - Exception Details: System.InvalidOperationException

我嘗試從我的兩個表中獲取所有數據,但我有很多編譯問題。 你知道它在哪里有問題嗎? 另外我想在這里聲明兩個表返回“View(repository.Towar);” 現在是可見的,但我不知道我該怎么做這個“se.Towar_zakupiony”。

我自己找到了解決辦法

碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Abstract;
using SportsStore.Entities;

namespace SportsStore.WebUI.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/
            public ITowarRepository repository;
            public IKategorieRepository re;

            public ProductController(ITowarRepository productRepository, IKategorieRepository kategorie) 
            {
            repository = productRepository;
            re = kategorie;
            }

            public ViewResult List()
            {
                SomeViewModel viewModel = new SomeViewModel
                {
                    Towar = repository.Towar
                    .OrderBy(p => p.Id_tow),
                    Kategorie = re.Kategorie
                    .OrderBy(p => p.Id_kat)

                };

                return View(viewModel);
            }

    }
}

碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SportsStore.Entities
{
    public class SomeViewModel
    {
        public IEnumerable<SportsStore.Entities.Towar> Towar { get; set; }
        public IEnumerable<SportsStore.Entities.Kategorie> Kategorie { get; set; }
        public IEnumerable<SportsStore.Entities.Zdjecie> Zdjecie { get; set; }
        public IEnumerable<SportsStore.Entities.Typ_zdjecia> Typ_zdjecia { get; set; }
        public IEnumerable<SportsStore.Entities.Zakup> Zakup { get; set; }
        public IEnumerable<SportsStore.Entities.Adres> Adres { get; set; }
        public IEnumerable<SportsStore.Entities.Status> Status { get; set; }
        public IEnumerable<SportsStore.Entities.Towar_Zakupiony> Towar_zakupiony { get; set; }


    }
}

碼:

@model SportsStore.Entities.SomeViewModel

@{
    ViewBag.Title = "List";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>List</h2>

@foreach (var p in Model.Towar)
{
<div class="item">
<h3>@p.Nazwa</h3>
@p.Opis
<h4>@p.Cena.ToString("c")</h4>
</div>
}

<h2>List</h2>

@foreach (var b in Model.Kategorie)
{
<div class="item">
<h3>@b.Nazwa</h3>
</div>
}

您的視圖期望控制器作為模型:

@model SportsStore.WebUI.Controllers.ProductController

改為

@model <WhateverTypeYourRepo(repository.Towar)IsReturning>

似乎是DbSet,那么您需要相應地更新您的視圖。

也許您應該考慮使用viewmodel,請參閱此相關的Stack Overflow帖子以獲取更多詳細信息。 不建議您將整個控制器傳遞給視圖。

看起來像你的repositary.Towar屬性返回一個Towar對象的集合。 所以你的觀點應該與此有關

@model IEnumerable<Towar>    
@{
    ViewBag.Title = "List";
}

<h2>List</h2>

@foreach (var p in Model)
{
 <div class="item">
   <h3>@p.Nazwa</h3>
   @p.Opis
   <h4>@p.Cena.ToString("c")</h4>
 </div>
}

假設NaszwaCenaTowar類的兩個屬性

建議:我個人覺得你的代碼可以更好閱讀。

1)我會在Repositary中編寫一個方法,它返回像這樣的Towar對象Collection

public IList<Towar> GetAllTowars();

您可以在Implementation類中實現它以返回Towar對象的List。 實施(獲取數據取決於您)

在我的控制器中,我會這樣稱呼它

var towerList=repo.GetAllTowars();
retuen View(towerList);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM