ASP MVC 3一种视图中的两种模型

时间:2013-08-01 18:52:36

标签: asp.net-mvc-3 viewmodel

我正在ASP MVC 3中创建一个datagrid,我在一个视图中有两个表。每个表都使用自己的模型。因此,我必须将两个模型调用到一个视图中,这看起来并不像我希望的那样简单。

我是MVC的新手,我正在浏览Stack并找到此链接: Two models in one view in ASP MVC 3

这似乎是我想要去的方向......我想。

以下是我的第一个模型的代码:

[Table]
public class Model1
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true)]
    public int Column1 { get; set; }
    [Column]
    public string Column2 { get; set; }
    [Column]
    public string Column3 { get; set; }
}

以下是我的第二个模型的代码:

[Table]
public class Model2
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true)]
    public int Column1 { get; set; }
    [Column]
    public int Column2 { get; set; }
    [Column]
    public string Column3  { get; set; }
    [Column]
    public string Column4 { get; set; }
}

以下是其他Stack Forum建议的父视图模型的代码:

public class ParentModelView
{
    public Model1 Model1 { get; set; }
    public Model2 Model2 { get; set; }
}

我能够让每个人单独工作,所以我知道这不是任何其他问题。另一个Stack Forum似乎对我的理解有点过于模糊。我觉得除了添加另一个使用其中的每个模型的父模型(我从中得到的东西)之外还有更多的东西。

您可能会发现有用的其他一些信息是每个模型都在自己的文件中。另外,这是我的错误:

The model item passed into the dictionary is of type 
'System.Data.Linq.Table 1[Model1]', but this dictionary requires a model 
item of type 'System.Collections.Generic.IEnumerable 1[ParentModelView]'.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information 
about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The model item passed 
into the dictionary is of type 'System.Data.Linq.Table 1[Model1]', but this 
dictionary requires a model item of type 
'System.Collections.Generic.IEnumerable 1[ParentModelView]'.

Source Error: 

An unhandled exception was generated during the execution of the current 
web request. Information regarding the origin and location of the exception 
can be identified using the exception stack trace below.

---编辑1 ---

以下是我的视图中的代码:

@model IEnumerable<ParentModelView>
@{
    WebGrid gridModel1 = new WebGrid(Model);
    WebGrid gridModel2 = new WebGrid(Model);
}
<div class="Table">
  <h2>Model1</h2>
  @gridModel1.GetHtml(columns: new[] {
      gridModel1.Column("Column1"),
      gridModel1.Column("Column2"),
      gridModel1.Column("Column3")
  })
</div>

<div class="Table" id="rightTable">
  <h2>Model2</h2>
  @*@gridModel2.GetHtml(columns: new[] {
      gridModel2.Column("Column1"),
      gridModel2.Column("Column2"),
      gridModel2.Column("Column3"),
      gridModel1.Column("Column4")
  })*@
</div>

编辑2

正如大多数人所要求的,这是我的控制器代码。我知道这是不对的,因为我不太确定如何将两个模型之间的信息传递到同一个视图中。如果有人能够提供帮助,我们将非常感激。

public class HomeController : Controller
{
    public ActionResult Index()
    {

        Model1Repo repoModel1 = new Model1Repo ();
        var Model1RepoSQL = repoModel1.All();

        Model2Repo repoModel2 = new Model2Repo();
        var Model2RepoSQL = repoModel2.All();

        return View(Model1RepoSQL);
    }
}

非常感谢任何其他信息。谢谢!

2 个答案:

答案 0 :(得分:3)

我认为你想要的更像是这样:

public class ParentModelView
{
    public IEnumerable<Model1> Model1 { get; set; }
    public IEnumerable<Model2> Model2 { get; set; }
}

在你看来

@model ParentModelView

@{
    WebGrid gridModel1 = new WebGrid(Model.Model1);
    WebGrid gridModel2 = new WebGrid(Model.Model2);
}

EDIT;

显然,您没有正确填充父模型。我以为你会明白怎么做。

public ActionResult MyAction() {
    var model1 = // get rows of model1
    var model2 = // get rows of model2

    return View("myview", new ParentModelView { Model1 = model1, Model2 = model2 }) ;
}

答案 1 :(得分:0)

在这种情况下,您始终可以使用ViewModel。例如,创建一个viewmodel

public class ViewModel{

    public int Table1Column1 { get; set; }
    public string Table1Column2 { get; set; }
    public string Table1Column3 { get; set; }


    public int Table2Column1 { get; set; }
    public int Table2Column2 { get; set; }
    public string Table2Column3  { get; set; }
    public string Table2Column4 { get; set; }

}

从业务层或数据访问层的两个模型中将数据导入ViewModel

P.S :McGarnagle所说的是正确的,但反之亦然,您将儿童模型发送到视图中,而期待ParentModelView。如果你可以发布控制器和视图的部分代码,那将会有所帮助。