两种模式或一个视图? [英] Two Models or One View?

查看:111
本文介绍了两种模式或一个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个视图中使用的两​​种车型。这里是我的code:

I want to use two models in one View. Here is my code:

public class Users {
   public int id { get; set; }
   public string adSoyad { get; set; }
   public string email { get; set; }
   public int puan { get; set; }
}
public class admin {
   public int id { get; set; }
   public string name { get; set; }
}

public class mainmodel {
   public Users Users { get; set; }
   public admin admin { get; set; }
}

我可以用它来删除,编辑和创建视图。但是,我得到我的索引视图错误:

I can use it to delete, edit, and create views. But I get errors in my index view:

@model IEnumerable<donemProje.Models.mainmodel>

我该怎么办?

Edit--
我尝试这在索引视图

Edit-- i try this in index view

@model donemProje.Models.mainmodel


@foreach (var item in Model.Users)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem =>item.adSoyad)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.email)

和得到这个错误

编译器错误信息:CS1579:foreach语句无法在类型donemProje.Models.Users'变量工作,因为donemProje.Models.Users'不包含'的GetEnumerator'一个公共定义

Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'donemProje.Models.Users' because 'donemProje.Models.Users' does not contain a public definition for 'GetEnumerator'

INT此行

34号线: @foreach(在Model.Users VAR项目)

推荐答案

@model IEnumrable 或你试过 @model IeEnumrable&LT; mainmodel&GT; ?如果你试过 @model IEnumrable&LT; mainmodel&GT; 比mainmodel有它的用户列表和管理。但我从你的问题理解,我认为下面的话会帮你。

@model IEnumrable or you tried @model IeEnumrable<mainmodel> ? if you tried @model IEnumrable<mainmodel> than mainmodel have list of users and admin in it. but what i understand from your question i think the below words will help you.

您可以尝试 @model mainmodel 键,你可以使用foreach循环通过迭代mainmodel

You can try @model mainmodel and you can iterate through mainmodel using foreach loop.

foreach(var user in Model.Users)
{
  // you code goes here..
} 

和因为如果要通过管理迭代管理员同样的事情。否则只是使用 @ Model.Admin.id

and same thing for the admin if want to iterate through admin. else just use @Model.Admin.id

这篇关于两种模式或一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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