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>
}
Looks like your
repositary.Towar
property returns a Collection of Towar object. So your view should bounded to thatAssuming
Naszwa
andCena
are two properties of yourTowar
classSuggestion : Personally i feel your code can be better readable.
1) I would write a method in the Repositary which returns a
Collection
ofTowar
objeccts like thisYou 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
Your view is expecting a controller as a model:
Change that to
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.