Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在MVC razor视图中使用两个以上的模型?_C#_Asp.net Mvc_Linq_Model_Controller - Fatal编程技术网

C# 如何在MVC razor视图中使用两个以上的模型?

C# 如何在MVC razor视图中使用两个以上的模型?,c#,asp.net-mvc,linq,model,controller,C#,Asp.net Mvc,Linq,Model,Controller,我已使用实体数据模型向导在SQL server中为我的表创建实体框架,但是我需要在MVC中的razor视图中使用两个以上的模型,目前我在以下位置有一个现有的linq查询: 控制器视图 var test = from a in db.tbl_users where a == 2 select a; return view (test.ToList()); Razor视图: @model IEnumerable <Telephone_Search.Models.

我已使用实体数据模型向导在SQL server中为我的表创建实体框架,但是我需要在MVC中的razor视图中使用两个以上的模型,目前我在以下位置有一个现有的linq查询:

控制器视图

var test = from a in db.tbl_users
       where a == 2
       select a;

return view (test.ToList());
Razor视图:

@model IEnumerable <Telephone_Search.Models.tbl_users>

@foreach (var item in model)

@HTML.DisplayFor(modelItem => item.users)
@model IEnumerable
@foreach(模型中的var项目)
@DisplayFor(modelItem=>item.users)
但是,我计划访问实体框架中存在的其他表,我希望循环访问这些表,类似这样的

@model IEnumerable <Telephone_Search.Models.'EntityFramework'>

@foreach (var item in 'entityframework')

@HTML.DisplayFor(modelItem => EntityFramework.'table'.'item')
@model IEnumerable
@foreach(“entityframework”中的变量项)
@DisplayFor(modelItem=>EntityFramework.table.'item')

我已尝试创建视图模型,但我的类在模型文件夹中是分开的,我不确定如何将这些类包装到一个视图模型中?

您必须创建一个新类,通常称为视图模型,并在该类中传递数据:

public class MyViewModel
{
  public IEnumerable<Telephone_Search.Models.tbl_users> users;
  public IEnumerable<OtherType> otherThings;
  public string SomeOtherProp {get;set;}
}

您必须创建一个新类,通常称为ViewModel,并在该类中传递数据:

public class MyViewModel
{
  public IEnumerable<Telephone_Search.Models.tbl_users> users;
  public IEnumerable<OtherType> otherThings;
  public string SomeOtherProp {get;set;}
}

直接来说,我们不能绑定多个模型,但可以使用其他选项来处理多个模型

  • 使用Viewbag可以传递其他模式的数据

     ViewBag.Users = db.Students.ToList();
    
     public class StudentViewModel
     {
          public string StudentName{get;set;}
          public IEnumerable<MarksheetViewModels> Results;          
     }
    
    在视图侧

     @{
         var students = (List<Students>)ViewBag.Students;
      }
    
    @{
    var students=(List)ViewBag.students;
    }
    
  • 可以使用包含其他模态对象的属性创建模态

     ViewBag.Users = db.Students.ToList();
    
     public class StudentViewModel
     {
          public string StudentName{get;set;}
          public IEnumerable<MarksheetViewModels> Results;          
     }
    
    公共类学生视图模型
    {
    公共字符串StudentName{get;set;}
    公众参与的结果不计其数;
    }
    

  • 直接来说,我们不能绑定多个模型,但可以使用其他选项来处理多个模型

  • 使用Viewbag可以传递其他模式的数据

     ViewBag.Users = db.Students.ToList();
    
     public class StudentViewModel
     {
          public string StudentName{get;set;}
          public IEnumerable<MarksheetViewModels> Results;          
     }
    
    在视图侧

     @{
         var students = (List<Students>)ViewBag.Students;
      }
    
    @{
    var students=(List)ViewBag.students;
    }
    
  • 可以使用包含其他模态对象的属性创建模态

     ViewBag.Users = db.Students.ToList();
    
     public class StudentViewModel
     {
          public string StudentName{get;set;}
          public IEnumerable<MarksheetViewModels> Results;          
     }
    
    公共类学生视图模型
    {
    公共字符串StudentName{get;set;}
    公众参与的结果不计其数;
    }
    

  • 实体数据模型向导生成的分部类中可能存在模型副本,我需要找到一种方法,将父视图模型与这些分部类一起放置。实体数据模型向导生成的分部类中可能存在模型副本,我需要找到一种方法来放置带有这些分部类的父视图模型。谢谢:)!帮了大忙!我很高兴这样做。但是,不要用评论来感谢别人。你只要把它标为正确并投票就足够了。摘自SO帮助:“使用评论询问更多信息或澄清问题或答案。”;)没关系,我指的是用户,而不是剃须刀中的tbl_用户。最后工作:)谢谢!也谢谢你,没问题谢谢:)!帮了大忙!我很高兴这样做。但是,不要用评论来感谢别人。你只要把它标为正确并投票就足够了。摘自SO帮助:“使用评论询问更多信息或澄清问题或答案。”;)没关系,我指的是用户,而不是剃须刀中的tbl_用户。最后工作:)谢谢!我也谢谢你,没问题