我想用两次@Model从我的网站的另一部分是有可能得到的数据?因为现在我有错误,但如果我只有这第一@model一切工作的正确。
外观 - > MVC 3 - 异常详细信息:System.InvalidOperationException
错误2'SportsStore.Entities.Kategorie'不包含'OPIS的定义,并没有扩展方法'OPIS接受型SportsStore.Entities.Kategorie'的第一个参数可以找到(是否缺少使用指令或程序集引用)C:\\用户\\拉法尔\\桌面\\ MVCksiązka\\司法部PROJEKT \\ SKLEP \\ SportsStore.WebUI \\查看\\产品\\ List.cshtml 16 4 SportsStore.WebUI
@model IEnumerable的< SportsStore.Entities.Towar>@model IEnumerable的< SportsStore.Entities.Kategorie>@ { ViewBag.Title =清单;}< H2>列表与LT; / H>@foreach(以型号VAR P){ < DIV CLASS =项> < H3> @ p.Nazwa< / H3 GT&; @ p.Opis < H4> @ p.Cena.ToString(C)< / H4> < / DIV>}解决方案
您只能有每个视图一个模型。但是你可以用另一目的是declarate模型:
公共类SomeViewModel{ 公共IEnumerable的< Towar> Towars; 公共IEnumerable的<类别及GT;分类; 公共SomeViewModel(IEnumerable的< Towar> towars,IEnumerable的<分类和GT;类){ Towars = towars; 类别=类别; }},然后用它在你看来是这样的:
@model SportsStore.Entities.SomeViewModel@foreach(在Model.Towars VAR项){ < DIV CLASS =项> < H3> @ p.Nazwa< / H3 GT&; @ p.Opis < H4> @ p.Cena.ToString(C)< / H4> < / DIV>}@foreach(在Model.Categories VAR项){ @ item.Name @ *或你需要什么到这里@ *}我也建议你在MVC中使用英文名字。它更清晰的阅读和理解。)
I would like to using two times @model to get data from another part of my website is it possible? Because now I have got error but if I have only this first @model everything working correct.
Look -> MVC 3 - Exception Details: System.InvalidOperationException
Error 2 'SportsStore.Entities.Kategorie' does not contain a definition for 'Opis' and no extension method 'Opis' accepting a first argument of type 'SportsStore.Entities.Kategorie' could be found (are you missing a using directive or an assembly reference?) c:\Users\Rafal\Desktop\MVC ksiązka\moj projekt\sklep\SportsStore.WebUI\Views\Product\List.cshtml 16 4 SportsStore.WebUI
@model IEnumerable<SportsStore.Entities.Towar> @model IEnumerable<SportsStore.Entities.Kategorie> @{ 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> }
解决方案
You only can have one Model per View. But you can use another object to declarate the model:
public class SomeViewModel { public IEnumerable<Towar> Towars; public IEnumerable<Category> Categories; public SomeViewModel(IEnumerable<Towar> towars, IEnumerable<Category> categories) { Towars = towars; Categories = categories; } }And then use it in your view like this:
@model SportsStore.Entities.SomeViewModel @foreach (var item in Model.Towars) { <div class="item"> <h3>@p.Nazwa</h3> @p.Opis <h4>@p.Cena.ToString("c")</h4> </div> } @foreach (var item in Model.Categories) { @item.Name @* or what you need down here *@ }I would also recommend you to use english names in MVC. It's more clear to read and understand ;).
更多推荐
MVC 3使用两次@model
发布评论