c# - ASP.Net MVC C# 另一个 View 模型中的两个 View 模型 - 如何在 View 中引用

标签 c# asp.net-mvc asp.net-mvc-3 razor

我正在尝试通过使用 ViewModel 来遵循建议的最佳实践。

在我的论坛应用程序中,我想发布帖子列表,并在屏幕底部添加一个文本框,以便可以发布回复。

所以在我看来,我需要一个 ViewModel 用于帖子列表,一个 ViewModel 用于要发布的回复(TopicId 和 Content)。

因此,我认为我需要在第三个 ViewModel 中将这两个 ViewModel 组合在一起,并在我的 View 中迭代 ViewModel 的 Posts 部分,然后在底部创建一个表单,其中有 Reply 部分ViewModel 的 - 回发仅将 ReplyViewModel 发送到 Controller 。

我的 ViewModel 是:

 public class PostViewModel
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Author { get; set; }
    public DateTime DateOfTopic { get; set; }
}

public class ReplyViewModel
{
    public int TopicId { get; set; }
    public string Content { get; set; }
}

public class PostListAndReplyVM
{
    public List<PostViewModel> PostViewModel { get; set; }
    public ReplyViewModel ReplyViewModel { get; set; }
}

在我的 Controller 中,我填充了 PostViewModel,然后创建一个空的 ReplyViewModel,然后将它们组合到一个 PostListAndReplyVM 中:

//
    // GET: /Post/List/5
    public ActionResult List(int id = 0)
    {
        // Populate PostViewModel

        var post = db.Posts.Include(x => x.Topic)
              .Select(p => new PostViewModel
              {
                  PostId = p.TopicId,
                  Title = p.Title,
                  Description = p.Content,
                  DateOfTopic = p.DateOfPost,
                  Author = p.Author
              }
             ).ToList();

        // Populate empty ReplyViewModel

        ReplyViewModel prvm = new ReplyViewModel();
        prvm.Content = "";
        prvm.TopicId = id;

        // Combine two viewmodels into a third, to send to the controller

        PostListAndReplyVM pvm = new PostListAndReplyVM();
        pvm.ReplyViewModel = prvm;
        pvm.PostViewModel = post;

        // Return combined ViewModel to the view

        return View(pvm);
    }

那么在我看来我有:

@model IEnumerable<centreforum.Models.PostListAndReplyVM>

<table class="table table-condensed table-striped table-hover table-bordered">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.PostId)
    </th>
....
....
@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.PostId)
    </td>

如何在发布到 View 的 PostListAndReplyVM 中引用两个单独的 ViewModel。例如。如果我认为它应该是这样的:

@Html.DisplayFor(modelItem => itemitem.PostViewModel.PostId)

...但这给出了错误:

“centreforum.Models.PostListAndReplyVM”不包含“PostId”的定义,并且找不到接受“centreforum.Models.PostListAndReplyVM”类型的第一个参数的扩展方法“PostId”

对于列表部分:

@foreach (var item in Model.PostViewModel) 

...给出错误:

“System.Collections.Generic.IEnumerable”不包含“PostViewModel”的定义,并且找不到接受“System.Collections.Generic.IEnumerable”类型的第一个参数的扩展方法“PostViewModel”

我确定我遗漏了一些简单的东西 - 感谢您的帮助,

标记

最佳答案

我认为您在 View 上犯了一个错误,您在 Controller 上返回了一个 PostListAndReplyVM,并且您的 View 引用了 PostListAndReplyVM 的 IEnumerable。

您必须更改 View 中声明的模型

@model centreforum.Models.PostListAndReplyVM

希望对你有帮助

关于c# - ASP.Net MVC C# 另一个 View 模型中的两个 View 模型 - 如何在 View 中引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17172463/

相关文章:

asp.net-mvc-3 - 模型绑定(bind)器和隐藏字段

c# - 通用映射器而不是有许多单独的映射器

C# 执行非查询 "Connection must be valid and open."

c# - 如何使 Excel 单元格内容显示不同的值

c# - 您最喜欢的 FxCop 规则是什么?

c# - 有什么方法可以将复选框列表绑定(bind)到 asp.net mvc 中的模型

asp.net-mvc - 如何通过属性过滤器在 MVC 中设置 Razor 布局?

c# - 使用在锁定工作站上打开的应用程序登录 Windows 7

c# - 如何更正运行 Controller 操作产生的路由?

c# - Orchard cms路由问题