将两个模型传递给部分视图asp.net mvc

时间:2017-06-23 18:42:33

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

我正在尝试将两个模型传递给视图。现在我有两个模型消息和评论。这些是在我传递给视图的ViewModel中声明的。直到那里一切正常但我创建了另一个包含消息和注释的模型MergeModels。此模型也在我的ViewModel中声明。但是当我尝试将MergeModels模型传递给局部视图时,它出错并且我收到错误。

  

可以键入Portfolio.ViewModels.MessageViewModel模型,不会隐式转换为System.Collections.IEnumerable。有明确的转换(是否有转换?)

此外,我现在将两个完整模型传递给此局部视图,而我不需要这两个模型的所有数据。有没有办法只将所需的数据传递给view / partialview?

我已经在互联网上看了一眼,但我无法让这个工作。

以下是我的模特

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 int Messages Messages {get; set;}
        public Messages MessagesId { get; set; }
        public DateTime WhenCreated { get; set; }

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

namespace Portfolio.Models
{
    public class MergeModel
    {
        public Messages Messages { get; set; }
        public Comments Comments { get; set; }
    }
}

我的ViewModel

namespace Portfolio.ViewModels
{
    public class MessageViewModel
    {
        public IEnumerable<Messages> Messages { get; set; }
        public IEnumerable<Comments> Comments { get; set; }
        public IEnumerable<MergeModel> MergeModel{ get; set; }
    }
}

我的控制器

namespace Portfolio.Controllers
{
    public class MessagesController : Controller
    {
        private ApplicationDbContext _context;

        public MessagesController()
        {
            _context = new ApplicationDbContext();
        }

        protected override void Dispose(bool disposing)
        {
            _context.Dispose();
        }
        public ActionResult Blog()
        {
            var viewModel = new MessageViewModel()
            {
            Messages = _context.messages.OrderByDescending(Messages => 
            Messages.WhenCreated),Comments = _context.comments

            };

            return View(viewModel);
        }

        public ActionResult Comment()
        {
            var comment = _context.comments.ToList();

            return View(_context.comments.OrderByDescending(Comments => 
            Comments.WhenCreated));
        }
        public ActionResult Post()
       {
        return View();
        }

    //This binds the objects from the database to the values from the view

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(FormCollection formValues)
        {
            try
            {
                Messages message = new Messages();
                message.Title = formValues["Title"];
                message.Body = formValues["editor"];

                Comments comment = new Comments();
                comment.Comments_body = formValues["editor" + 
                message.MessagesId];

                _context.comments.Add(comment);
                _context.SaveChanges();
                _context.messages.Add(message);
                _context.SaveChanges();

            }
            catch
            {
                return View();
            }
            return RedirectToAction("Blog");
        }
    }
}

这是我的观点

@model 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/Comments.js"></script>
<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.Messages)
                {
                    <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.MergeModel)

@*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>
</div>

这里是我的PartialView

@{
    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("editor", 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('editor');
    </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)
                {
                    if (comments.CommentsId == messages.MessagesId)
                    {
                        <div class="well" id="CommentBox">
                        <h7>@comments.WhenCreated</h7>

        @Html.Raw(System.Web.HttpUtility.HtmlDecode(comments.Comments_body))
                    </div>
                    }
                    else
                    {
                        <h2>Be the first to react!</h2>
                    }
                }
            </div>
        </div>
    </div>
</div>

另外,我不确定如何在这部分中调用模型

  

@foreach(模型中的var评论)

因为我将视图传递给我的部分视图

  

@ Html.Partial(&#34; _Comment&#34;,Model.MergeModel)

因此,不仅模型我尝试了不同的变体,但它没有用于例如

  

@foreach(在Model.MergeModel中的var注释)

修改 因为我不够清楚,  我试图用foreach语句显示属于消息的所有注释。我首先使用它,但它显示了所有消息的所有注释,而不是仅显示属于其消息的注释。这就是我在局部视图中需要这两个模型的原因。我试图让评论只显示它所属的消息。这是我尝试使用if语句,其中id必须与注释和消息相同

修改 改变了代码中的错误,

if (comments.CommentsId == messages.MessagesId)

if (comments.MessagesId == messages.MessagesId)

并添加了

public Messages Messages{ get; set;}

在我的消息模型中

我对Asp.net mvc和stackoverflow都很陌生,所以如果我的问题有任何改进请告诉我。

0 个答案:

没有答案