1

我正在努力在 ASP MVC 3 中创建一个数据网格,其中一个视图中有两个表。每个表都从其自己的模型中使用。因此,我不得不将两个模型调用到一个视图中,这看起来并不像我希望的那么简单。

我对 MVC 很陌生,我正在查看 Stack 并找到了这个链接: 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; }
}

这是其他堆栈论坛建议的父视图模型的代码:

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

我能够让每个人单独工作,所以我知道这不是任何其他问题。另一个堆栈论坛对我的理解来说似乎有点太模糊了。我觉得除了添加另一个使用其中每个模型的父模型(我从中得到什么)之外,还必须做更多的事情。

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

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);
    }
}

任何其他信息将不胜感激。谢谢!

4

2 回答 2

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);
}

编辑;

显然,您没有正确填充父模型。我以为你会明白如何做到这一点。

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

    return View("myview", new ParentModelView { Model1 = model1, Model2 = model2 }) ;
}
于 2013-08-01T19:37:18.767 回答
0

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数据。

PS:McGarnagle 说的是对的,但反之亦然,您在期待时将子模型发送到视图中ParentModelView。如果您可以发布控制器和视图的部分代码,那将会很有帮助。

于 2013-08-01T19:28:38.597 回答