【问题标题】:ASP MVC 3 Two Models in One ViewASP MVC 3 一个视图中的两个模型
【发布时间】:2013-08-02 20:34:54
【问题描述】:

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

我对 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; }
}

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

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

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

【问题讨论】:

  • 错误表明视图期待子模型,而不是父模型。你能发布你观点的相关部分吗?
  • @McGarnagle - 请在我的帖子中查看我的视图中的代码编辑。
  • 您能否也发布操作方法,以便我们看到您传递给视图的内容?
  • 需要查看控制器内部的代码,即返回此视图。

标签: asp.net-mvc-3 viewmodel


【解决方案1】:

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

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

【讨论】:

  • 我试过你的解决方案,但它似乎仍然有同样的错误。
  • @jpriff - 这意味着您没有将正确的模型数据传递给视图。您需要向我们展示您的控制器代码。
  • 谢谢!您的最后一次编辑成功了。我不太确定如何填充父模型。我是 MVC 的总 n00b...
【解决方案2】:

在这种情况下,您始终可以使用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。如果您可以发布控制器和视图的部分代码,那将很有帮助。

【讨论】:

    猜你喜欢
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    相关资源
    最近更新 更多