问题描述
我想从我的两个表中获取所有数据,但我有很多的编译问题。你知道它在哪里的问题?此外,我想在这里声明两个表回归视图(repository.Towar);现在是可见的一个,但我不`吨知道我应该怎么做也是这个se.Towar_zakupiony。
我发现自己的解决方案:
code:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用SportsStore.Abstract;
使用SportsStore.Entities;命名空间SportsStore.WebUI.Controllers
{
公共类ProductController的:控制器
{
//
// GET:/产品/
公共ITowarRepository库;
公共IKategorieRepository重; 公共ProductController的(ITowarRepository productRepository,IKategorieRepository kategorie)
{
库= productRepository;
重新= kategorie;
} 公众的ViewResult列表()
{
SomeViewModel视图模型=新SomeViewModel
{
Towar = repository.Towar
.OrderBy(P => p.Id_tow)
Kategorie = re.Kategorie
.OrderBy(P => p.Id_kat) }; 返回视图(视图模型);
} }
}
code:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;命名空间SportsStore.Entities
{
公共类SomeViewModel
{
公共IEnumerable的< SportsStore.Entities.Towar> Towar {搞定;组; }
公共IEnumerable的< SportsStore.Entities.Kategorie> Kategorie {搞定;组; }
公共IEnumerable的< SportsStore.Entities.Zdjecie> Zdjecie {搞定;组; }
公共IEnumerable的< SportsStore.Entities.Typ_zdjecia> Typ_zdjecia {搞定;组; }
公共IEnumerable的< SportsStore.Entities.Zakup> Zakup {搞定;组; }
公共IEnumerable的< SportsStore.Entities.Adres>住址{搞定;组; }
公共IEnumerable的< SportsStore.Entities.Status>状态{搞定;组; }
公共IEnumerable的< SportsStore.Entities.Towar_Zakupiony> Towar_zakupiony {搞定;组; }
}
}
code:
@model SportsStore.Entities.SomeViewModel@ {
ViewBag.Title =清单;
布局=〜/查看/共享/ _Layout.cshtml
}< H2>列表与LT; / H>@foreach(在Model.Towar VAR P)
{
< DIV CLASS =项>
< H3> @ p.Nazwa< / H3 GT&;
@ p.Opis
< H4> @ p.Cena.ToString(C)< / H4>
< / DIV>
}< H2>列表与LT; / H>@foreach(在Model.Kategorie变种B)
{
< DIV CLASS =项>
< H3> @ b.Nazwa< / H3 GT&;
< / DIV>
}
看起来像你的 repositary.Towar
属性返回Towar收集一个对象。所以,你的看法应该有界到
@model IEnumerable的< Towar>
@ {
ViewBag.Title =清单;
}< H2>列表与LT; / H>@foreach(以型号VAR P)
{
< DIV CLASS =项>
< H3> @ p.Nazwa< / H3 GT&;
@ p.Opis
< H4> @ p.Cena.ToString(C)< / H4>
< / DIV>
}
假设 Naszwa
和以上的价格
是你的 Towar $ C $的两个属性C>类
建议:我个人觉得你的code可以得到更好的可读性
1)我会写在Repositary方法,该方法返回一个收藏
Towar的
objeccts这样
公开的IList< Towar> GetAllTowars();
您可以在您的实现类实现这个返回Towar对象的名单。实施(获取数据是由你)
这是在我的控制,我将这样称呼它
VAR towerList = repo.GetAllTowars();
retuen视图(towerList);
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 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);
这篇关于MVC 3 - 异常详细信息:System.InvalidOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!