MVC 3使用两次@model [英] MVC 3 using @model twice

查看:160
本文介绍了MVC 3使用两次@model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用两次@Model从我的网站的另一部分是有可能得到的数据?因为现在我有错误,但如果我只有这第一@model一切工作的正确。

外观 - > <一个href=\"http://stackoverflow.com/questions/11810558/mvc-3-exception-details-system-invalidoperationexception\">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的&LT; SportsStore.Entities.Towar&GT;
@model IEnumerable的&LT; SportsStore.Entities.Kategorie&GT;
@ {
    ViewBag.Title =清单;
}&LT; H2&GT;列表与LT; / H&GT;@foreach(以型号VAR P)
{
    &LT; D​​IV CLASS =项&GT;
         &LT; H3&GT; @ p.Nazwa&LT; / H3 GT&;
         @ p.Opis
         &LT; H4&GT; @ p.Cena.ToString(C)&LT; / H4&GT;
    &LT; / DIV&GT;
}


解决方案

您只能有每个视图一个模型。但是你可以用另一目的是declarate模型:

 公共类SomeViewModel
{
   公共IEnumerable的&LT; Towar&GT; Towars;
   公共IEnumerable的&LT;类别及GT;分类;   公共SomeViewModel(IEnumerable的&LT; Towar&GT; towars,IEnumerable的&LT;分类和GT;类){
     Towars = towars;
     类别=类别;
   }
}

,然后用它在你看来是这样的:

  @model SportsStore.Entities.SomeViewModel@foreach(在Model.Towars VAR项)
{
  &LT; D​​IV CLASS =项&GT;
    &LT; H3&GT; @ p.Nazwa&LT; / H3 GT&;
    @ p.Opis
    &LT; H4&GT; @ p.Cena.ToString(C)&LT; / H4&GT;
  &LT; / DIV&GT;
}
@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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆