MVC 3使用两次@model

编程入门 行业动态 更新时间:2024-10-25 03:24:54
本文介绍了MVC 3使用两次@model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用两次@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){    < D​​IV 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项){  < D​​IV 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

本文发布于:2023-11-27 05:22:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636767.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   MVC   model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!