C# 将数据从ViewModel传递到View和PartialView时出现问题

C# 将数据从ViewModel传递到View和PartialView时出现问题,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,我正在建立一个非常简单的博客。对于我的博客,我需要两个模型:评论和消息。我知道在视图中使用两个模型需要一个ViewModel,但我有一些困难。由于某些原因,我无法访问这两个模型,我的局部视图也是如此。现在我对asp.NETMVC还很陌生,知道这可能是一个愚蠢的错误,但我希望有人能告诉我我做错了什么,最重要的是为什么 这是我的模型 namespace Portfolio.Models { public class Messages { public int Mess

我正在建立一个非常简单的博客。对于我的博客,我需要两个模型:评论和消息。我知道在视图中使用两个模型需要一个ViewModel,但我有一些困难。由于某些原因,我无法访问这两个模型,我的局部视图也是如此。现在我对asp.NETMVC还很陌生,知道这可能是一个愚蠢的错误,但我希望有人能告诉我我做错了什么,最重要的是为什么

这是我的模型

namespace Portfolio.Models
{
    public class Messages
    {
        public int MessagesId { get; set; }
        [Required]
        public string Title { get; set; }
        [Required]
        public string Body { get; set; }
        public DateTime WhenCreated { get; set; }

        public Messages()
        {
            WhenCreated = DateTime.Now;
        }
    }
}

namespace Portfolio.Models
{
    public class Comments
    {
        public int CommentsId { get; set; }
        public string Comments_body { get; set; }
        public Messages MessagesId { get; set; }
        public DateTime WhenCreated { get; set; }

        public Comments()
        {
            WhenCreated = DateTime.Now;
        }
    }
}
这是我的ViewModel

namespace Portfolio.ViewModels
{
    public class MessageViewModel
    {
        public IEnumerable<Messages> Messages { get; set; }
        public IEnumerable<Comments> Comments { get; set; }
    }
}
以下是我的观点

@model IEnumerable<Portfolio.ViewModels.MessageViewModel>

@{
    ViewBag.Title = "Blog";
    Layout = "~/Views/Shared/_Layout.cshtml";
 }
<link rel="stylesheet" type="text/css" href="~/Content/css/Blog.css" />
<script src="~/Scripts/ckeditor/ckeditor.js"></script>

<div class="jumbotron opacity_container">
    <div class="row">
        <div class="col-md-12">
            <h2>Latest Posts</h2>
            <div class="row">
                <div class="col-md-12">
                    @foreach (var messages in Model)
                    {
                        <div class="jumbotron opacity_container">
                            <div class="col-md-12">
                                <div class="panel panel-primary">
                                    <div class="panel-heading">
                                        @*Gets the title of the blog post*@
                                        <h2 class="panel-title">
                                        @messages.Title
                                        </h2>@messages.WhenCreated
                                   </div>
@*Gets the body of the blog post and decodes the html of the ckeditor*@

                                <div class="panel-body">
                 @Html.Raw(System.Web.HttpUtility.HtmlDecode(messages.Body))
                                </div>
                            </div>
                        </div>
 @*this button gets the id from the database of the 
 Message table this helps to prevent that all the comments from all blogs 
 gets shown and thus shows only the comments that belong to the blog in 
 question*@

                    <button class="btn btn primary"id="@messages.MessagesId"
                    onclick="ShowComments(this.id)">
                    Show Comments
                    </button>

@*this is the container where al the comments are placed in and where you 
can post comments. The comments are placed in the Comment partial view*@
                        <div class="hidden" id="Comm@(messages.MessagesId)">
                            @Html.Partial("_Comment", Model)

@*this button gets the id from the database of the Message table this helps 
to prevent that all the comments from all blogs gets hidden and thus
hides only the comments that belong to the blog in question*@
                        <button class="btn btn-primary" 
                        id="@messages.MessagesId" 
                        onclick="HideComments(this.id)">Hide Comments
                        </button>
                    </div>
                </div>
                }
            </div>
        </div>
    </div>
</div>
@{
   ViewBag.Title = "Home Page";
}
<link rel="stylesheet" type="text/css" href="~/Content/css/Blog.css" />

<div class="row" id="CommentContainer">
    <div class="col-md-12">
        <h3>Post Comment</h3>
 @*The form to post comments*@
        @using (Html.BeginForm("Create", "Messages"))
            {
            <div class="form-group">
                <label>Comment</label>
                @Html.TextArea("editor1", htmlAttributes: new { name = 
               "editor1", id = "editor", rows = "10", cols = "180" })
            </div>
                <button type="submit" class="btn btn-primary" 
              id="PostButton">Post Comment</button>
            }

 @*CKEdito script*@
        <script>
             CKEDITOR.replace('editor1');
        </script>


        <div class="row">
            <div class="col-md-10">
@*Places al the comments and decodes the html from the ckeditor*@
                @foreach(var comments in Model)
                {
                    <div class="well" id="CommentBox">

@Html.Raw(System.Web.HttpUtility.HtmlDecode(comments.Comments_body))
                    </div>
                }
            </div>
        </div>
    </div>
</div>
@model IEnumerable
@{
ViewBag.Title=“博客”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
最新帖子
@foreach(模型中的var消息)
{
@*获取博客文章的标题*@
@信息,标题
@消息。当创建时
@*获取博客文章的正文并解码该编辑器的html*@
@Html.Raw(System.Web.HttpUtility.HtmlDecode(messages.Body))
@*此按钮从数据库中获取
消息表这有助于防止来自所有博客的所有评论被删除
显示,因此仅显示中属于博客的评论
问题:*@
显示评论
@*这是所有注释所在的容器,也是您
可以发表评论。评论放置在评论部分视图中*@
@Html.Partial(“_Comment”,Model)
@*此按钮从消息表的数据库中获取id,这有助于
防止来自所有博客的所有评论被隐藏,从而
仅隐藏属于相关博客的评论*@
隐藏评论
}

这是我的部分观点

@model IEnumerable<Portfolio.ViewModels.MessageViewModel>

@{
    ViewBag.Title = "Blog";
    Layout = "~/Views/Shared/_Layout.cshtml";
 }
<link rel="stylesheet" type="text/css" href="~/Content/css/Blog.css" />
<script src="~/Scripts/ckeditor/ckeditor.js"></script>

<div class="jumbotron opacity_container">
    <div class="row">
        <div class="col-md-12">
            <h2>Latest Posts</h2>
            <div class="row">
                <div class="col-md-12">
                    @foreach (var messages in Model)
                    {
                        <div class="jumbotron opacity_container">
                            <div class="col-md-12">
                                <div class="panel panel-primary">
                                    <div class="panel-heading">
                                        @*Gets the title of the blog post*@
                                        <h2 class="panel-title">
                                        @messages.Title
                                        </h2>@messages.WhenCreated
                                   </div>
@*Gets the body of the blog post and decodes the html of the ckeditor*@

                                <div class="panel-body">
                 @Html.Raw(System.Web.HttpUtility.HtmlDecode(messages.Body))
                                </div>
                            </div>
                        </div>
 @*this button gets the id from the database of the 
 Message table this helps to prevent that all the comments from all blogs 
 gets shown and thus shows only the comments that belong to the blog in 
 question*@

                    <button class="btn btn primary"id="@messages.MessagesId"
                    onclick="ShowComments(this.id)">
                    Show Comments
                    </button>

@*this is the container where al the comments are placed in and where you 
can post comments. The comments are placed in the Comment partial view*@
                        <div class="hidden" id="Comm@(messages.MessagesId)">
                            @Html.Partial("_Comment", Model)

@*this button gets the id from the database of the Message table this helps 
to prevent that all the comments from all blogs gets hidden and thus
hides only the comments that belong to the blog in question*@
                        <button class="btn btn-primary" 
                        id="@messages.MessagesId" 
                        onclick="HideComments(this.id)">Hide Comments
                        </button>
                    </div>
                </div>
                }
            </div>
        </div>
    </div>
</div>
@{
   ViewBag.Title = "Home Page";
}
<link rel="stylesheet" type="text/css" href="~/Content/css/Blog.css" />

<div class="row" id="CommentContainer">
    <div class="col-md-12">
        <h3>Post Comment</h3>
 @*The form to post comments*@
        @using (Html.BeginForm("Create", "Messages"))
            {
            <div class="form-group">
                <label>Comment</label>
                @Html.TextArea("editor1", htmlAttributes: new { name = 
               "editor1", id = "editor", rows = "10", cols = "180" })
            </div>
                <button type="submit" class="btn btn-primary" 
              id="PostButton">Post Comment</button>
            }

 @*CKEdito script*@
        <script>
             CKEDITOR.replace('editor1');
        </script>


        <div class="row">
            <div class="col-md-10">
@*Places al the comments and decodes the html from the ckeditor*@
                @foreach(var comments in Model)
                {
                    <div class="well" id="CommentBox">

@Html.Raw(System.Web.HttpUtility.HtmlDecode(comments.Comments_body))
                    </div>
                }
            </div>
        </div>
    </div>
</div>
@{
ViewBag.Title=“主页”;
}
发表评论
@*发布评论的表单*@
@使用(Html.BeginForm(“创建”、“消息”))
{
评论
@TextArea(“editor1”,htmlAttributes:new{name=
“editor1”,id=“editor”,rows=“10”,cols=“180”})
发表评论
}
@*CKEdito脚本*@
CKEDITOR.replace('editor1');
@*放置所有注释并从编辑器中解码html*@
@foreach(模型中的var注释)
{
@Html.Raw(System.Web.HttpUtility.HtmlDecode(comments.comments\u body))
}
此PartialView显示评论,而视图显示博客消息。 因此,我需要将数据从我的消息模型发送到我的视图,并且我需要在我的部分视图中从我的注释模型发送和检索数据


谢谢你的帮助

要在视图中使用ViewModel,需要将其从控制器传递到视图。您当前正在传递一组消息:

 public ActionResult Blog()
    {
        var messages = _context.messages.ToList();

        // Your view is expecting a parameter of type MessageViewModel,
        // but you're passing it an object of type List<Messages>
        return View(_context.messages.OrderByDescending(Messages => 
        Messages.WhenCreated));
    }
您还需要更新视图:

@model Portfolio.ViewModels.MessageViewModel
...
@foreach (var messages in Model.Messages)
...
@Html.Partial("_Comment", Model.Comments)
你需要让你的局部视图取一个参数
IEnumberable
作为其型号:

@model IEnumerable<Portfolio.Models.Comments>
@model IEnumerable

请注意,此解决方案当前正在从上下文中获取所有消息和所有注释,并将它们传递给视图。您可能希望对其进行筛选(仅传入附加到特定消息的注释等)。

您现在是否收到任何类型的错误消息?如果是,您可以共享它吗?这是错误消息“i get MessageViewModel”不包含“Title”的定义,并且找不到接受“MessageViewModel”类型的第一个参数的扩展方法“Title”(您是否缺少using指令或程序集引用?)您已将视图的model属性设置为collection ViewModel类,但您正在向视图发送注释或消息集合我已应用了您的建议,但仍然收到相同的错误。我只是更改了控制器而不是视图,所以可能我在视图中称其为错误?我只是更新了答案,以包括对视图和部分视图的一些更改。thnx将立即尝试。我对c#和asp.net都是新手,非常感谢您的帮助,所以我尝试过它,在visual studio中,当我将鼠标悬停在@messages.WhenCreated上时,它会显示:DateTime Portfolio.Models.messages.WhenCreated{get;set;}因此,我认为它传递正确,但当我编译时,我现在得到错误IEnumerable'不包含'Messages'的定义,并且找不到接受'IEnumerable'类型的第一个参数的扩展方法'Messages'(您是否缺少using指令或程序集引用?)错误指向此部分:@foreach(Model.messages中的var messages)我觉得很奇怪,因为在visual studio中它显示它是正确传递的