我试图从我的两个表中获取所有数据,但我有很多编译问题。你知道问题出在哪里吗?另外我想在这里声明两个表 return "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>
}