【问题标题】:How to pass two objects of different class to the View's model如何将不同类的两个对象传递给视图的模型
【发布时间】:2014-08-25 15:21:40
【问题描述】:

在一个人的Details 视图中,我还想显示DbContext 中的其他对象,确切地说是表Survey 的每条记录。所以我将它们插入ViewBag

人的控制器动作:

   [HttpPost]
        public ActionResult _Survey1(int id) {
            System.Diagnostics.Debug.WriteLine("PASSED ID: " + id);
            Person person = db.Persons.Find(id);
            ViewBag.Surveys = db.Surveys.ToList();//HERE I insert Surveys to ViewBag
            return PartialView(person);
        }

并尝试在 View 中迭代它们:

@model WebApplication2.Models.Person
<hr />
<h2>Survey</h2>

<input type="button" id="Coll" value="Collapse" onclick="javascript:CollapseDiv()" />
<p>
    @Html.DisplayFor(x => ViewBag.Surveys.Count)
</p>
@foreach (var survey in ViewBag.Surveys) {
    <p>
        @using (Html.BeginForm()) {

            <text>ANK</text>
            @Html.DisplayFor(x => survey.Questions.Count)
            @Html.EditorFor(x => survey.Questions) 
            <input type="submit" />
        }
    </p>
}

<hr />

问题是我得到了:

问题:正如 tschmit007 建议的那样,如何定义可以接受 Person 和 `IEnumerable 并将数据传递给它的模型?

【问题讨论】:

  • @Html.DisplayFor(x =&gt; ViewBag.Surveys.Count) 期望您使用模型的一部分,您希望它生成什么?填充了计数的字段?
  • @siva.k 我想在表Survey 中显示行数。如果我在控制器中,我会写 db.Surveys.Count 而不是 ViewBag.Surveys.Count
  • 编辑你的模型:添加一个IEnumerable&lt;Survey&gt; Surveys到它;保留强类型视图。
  • @tschmit007 如何在模型中添加另一个字段?你能告诉我视图中的模型应该是什么样子以及如何传递这个额外的控制器吗?
  • 你编辑WebApplication2\Models\Person.cs文件。添加属性,然后在控制器级别填充它。那就是您按照 CodeCaster 的建议创建一个 ViewModel。

标签: .net asp.net-mvc entity-framework razor viewbag


【解决方案1】:

定义一个包含PersonSurveys 属性的视图模型。例如

public class MyViewModel
{
  public Person Person { get; set; } // or create properties for each property or Person
  public IEnumerable<Survey> Surveys { get; set; }
}

显示页面时

[HttpGet]
public ActionResult Details (int ID)
{
  MyViewModel model = new MyViewModel();
  model.Person = db.Persons.Find(id);
  model.Surveys = db.Surveys;
  return View(model);
}

在视图中

@model YourAssembly.MyViewModel

@Html.DisplayFor(m => m.Person.SomeProperty) // display properties of person

@foreach (var survey in Model.Surveys) {
  @using (Html.BeginForm()) {
        @Html.EditorFor(x => survey.Questions) 
        <input type="submit" />
  }
}

【讨论】:

  • 谢谢你这是一个很好的答案!但我也发布了我昨天使用的第二种方法的答案。我用了Tuple
【解决方案2】:

我将数据作为元组传递,但没有创建 ViewModel。元组出现在 .NET 4.0 中。这完成了工作:

 public ActionResult _Survey1(int id) {
            System.Diagnostics.Debug.WriteLine("PASSED ID: " + id);
            Person person = db.Persons.Find(id);
            var tuple = new Tuple<Person,  List<Survey>>(person, db.Surveys.ToList()){};               
            return PartialView(tuple);
        }

@using WebApplication2.Models
@model   System.Tuple<Person, List<Survey>>

<hr />
<h2>Surveys</h2>

<input type="button" id="Coll" value="Collapse" onclick="javascript:CollapseDiv()" />
<p>
    @Html.DisplayFor(x => Model.Item2.Count)
</p>

@foreach (var survey in Model.Item2) {
    using (Html.BeginForm()) {
        <text>Number of Questions in a Survey</text>
        @Html.DisplayFor(x => survey.Questions.Count)
        <p/>
        @Html.EditorFor(x => survey.Questions)
        <input type="submit" />
    }

}
<hr />

【讨论】:

    【解决方案3】:

    @Html.DisplayFor(x =&gt; ViewBag.Surveys.Count) 期望你使用模型的一部分,你有ViewBag 应该是x。您需要做的是删除 For 使其不使用该模型。

    改为使用:

    @Html.Display(ViewBag.Surveys.Count) 
    
    @Html.Display(survey.Questions.Count)
    @Html.Editor(survey.Questions)
    

    Display 很可能一起被排除在外:

    <p>@ViewBag.Surveys.Count</p>
    

    也可以。

    【讨论】:

    • 这些辅助方法有问题:s15.postimg.org/xwbvonwez/image.png
    • @Yoda type 是什么 survey.Questions?如果不是string,它将无法工作,您必须将其分解为各个字段。
    • @Yoda 将其拆分为部分视图可能会更好,而不是通过ViewBag 传递,因为如果您传递一堆数据来自ViewBag
    • QuestionsICollection&lt;Question&gt; Ok, so how to define model which has one field Person` 和第二个 IEnumerable&lt;Survey&gt;?
    • 已经是部分我想将PersonIEnumberable&lt;Survey&gt;传递给对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 2014-06-28
    • 1970-01-01
    相关资源
    最近更新 更多