簡體   English   中英

是否可以在一個asp.net mvc視圖中使用兩個模型

[英]Is it possible to have two models in one asp.net mvc view

我正在制作一個使用多個標簽的網絡應用程序。 我希望在一個選項卡中顯示一種類型的數據,例如在一個選項卡中顯示學生配置文件在其他選項卡中需要不同的模型,例如我需要在同一視圖中使用注冊模型類

是的,您可以使用System.Tuple類(或)通過使用ViewModel (自定義類)

ViewModel選項

public class ViewModel1
{
  public StudentProfile profile {get; set;}
  public Registration Reg {get; set;}
}

將此ViewModel1作為模型對象傳遞給視圖(或)將視圖與此模型對象綁定。 它比使用Tuple<T1, T2>更好

是的,您可以添加主視圖模型,然后將選項卡模型嵌套在主視圖模型中

public class YourMainViewModel
{
    public Tab1ViewModel Tab1 {get; set;}
    public Tab2ViewModel Tab2 {get; set;}
}

然后在您的視圖中,將標簽模型傳遞給您的部分(如果您使用部分標簽)

@{Html.RenderPartial("Tab1", Model.Tab1);}
@{Html.RenderPartial("Tab2", Model.Tab2);}

是的,我知道至少有3種方法可以在一個視圖中設置一些模型,但我認為最好的做法是使用ViewModels。 如果您使用實體框架,您可以做類似的事情。

在Models中設置一個將成為ViewModel的類。

public class StudensP_Registration
{
    public IEnumerable<StudentsProfile> studentsp { get;  set; }
    public IEnumerable<Registration> registration { get; set; }

}

然后你就像這樣在控制器中調用它。

public ActionResult Index()
    {

        StudensP_Registration oc = new StudensP_Registration();
        oc.studentsp = db.StudentsProfile;
        oc.registration = db.Registration;
        return View(oc);
    }

然后在視圖中

@using Proyect_name_space.Models;
@model StudensP_Registration
   @foreach (ordenes_compra item in Model.studentsp) {#something you      want..}

我希望我能幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM