MVC 3 - Exception Details: System.InvalidOperation

2019-06-14 01:42发布

I trying get all data from my two tables but I have a lot of compile problems. Do you know where is it problem? Also I would like to declaration two tables here return "View(repository.Towar);" Now is visible one but I don`t know what should I do also this "se.Towar_zakupiony".

I found solution myself:

code:

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);
            }

    }
}

code:

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; }


    }
}

code:

@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>
}

2条回答
叛逆
2楼-- · 2019-06-14 01:56

Looks like your repositary.Towar property returns a Collection of Towar object. So your view should bounded to that

@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>
}

Assuming Naszwa and Cena are two properties of your Towar class

Suggestion : Personally i feel your code can be better readable.

1) I would write a method in the Repositary which returns a Collection of Towar objeccts like this

public IList<Towar> GetAllTowars();

You may implement this in your Implementation class to return a List of Towar objects. Implementation (getting the data is up to you)

An in my controller i will call it like this

var towerList=repo.GetAllTowars();
retuen View(towerList);
查看更多
Animai°情兽
3楼-- · 2019-06-14 02:01

Your view is expecting a controller as a model:

@model SportsStore.WebUI.Controllers.ProductController

Change that to

@model <WhateverTypeYourRepo(repository.Towar)IsReturning>

Is appears to be a DbSet, then you'll need to update your view accordingly.

Maybe you should consider using a viewmodel, see this related Stack Overflow post for more details. It's not recommended that you pass your entire controller to a view.

查看更多
登录 后发表回答