加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.Net MVC C#另一个viewmodel中的两个viewmodel – 如何在视图

发布时间:2020-12-16 09:53:27 所属栏目:asp.Net 来源:网络整理
导读:我试图通过使用ViewModels来遵循建议的最佳实践. 在我的论坛应用程序中,我想发布一个帖子列表,并在屏幕底部添加一个文本框,以便发布回复. 所以在我看来,我需要一个ViewModel用于Posts列表,一个ViewModel用于发布Reply(TopicId和Content). 因此,我认为我需要
我试图通过使用ViewModels来遵循建议的最佳实践.

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

所以在我看来,我需要一个ViewModel用于Posts列表,一个ViewModel用于发布Reply(TopicId和Content).

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

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

在我的控制器中,我填充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>

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

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

…但是这给出了错误:

‘centreforum.Models.PostListAndReplyVM’不包含’PostId’的定义,也没有扩展方法’PostId’接受’centreforum.Models.PostListAndReplyVM’类型的第一个参数’

对于列表部分:

@foreach (var item in Model.PostViewModel)

…给出错误:

‘System.Collections.Generic.IEnumerable’不包含’PostViewModel’的定义,并且没有扩展方法’PostViewModel’接受类型’System.Collections.Generic.IEnumerable’的第一个参数可以找到

我确定我错过了一些简单的东西 – 感谢您的帮助,

标记

解决方法

我认为你的视图出错,你在控制器上返回一个PostListAndReplyVM,你的视图引用了一个IEnumerable的PostListAndReplyVM.

您必须在视图中更改声明的模型

@model centreforum.Models.PostListAndReplyVM

希望它有所帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读