请问如何在视图中使用2个模型 [英] Please How Do I Use 2 Models In A View

查看:67
本文介绍了请问如何在视图中使用2个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的评论模型如下:



 {
公开 UserComment
{
public UserComment()
{
DateUserCommented = DateTime.Now;
}
public int UserCommentId {获得; set ; }

[必需(ErrorMessage = 字段是必需的)]
[显示(名称= 评论信息)]
public string UserCommentSubject { get ; set ; }

[显示(名称= 评论人员)]
public string UserCommentedBy { get ; set ; }

public string UserCommentFile {获得; set ; }

[显示(名称= 日期评论)]
public DateTime DateUserCommented { get ; set ; }

public int UserId {获得; set ; }
public UserProfile UserProfiles { get ; set ; }

public int PostId {获得; set ; }
public virtual 帖子帖子{ get < /跨度>; set ; }

// public int QuoteId {get;组; }
// public virtual Quote Quotes {get;组; }

public virtual ICollection< Quote>引号{获取; set ; }

}
}





我的报价模型是这样的:





 {
public class 引用
{
public Quote()
{
DateQuoted = DateTime.Now;
}
public int QuoteId {获得; set ; }

[必需]
public string QuoteContent {获得; set ; }

[显示(名称= 回复者)]
public string QuotedBy { get ; set ; }

[显示(名称= 日期引用)]
public DateTime DateQuoted { get ; set ; }

public string QuoteFile {获得; set ; }

public int UserId {获得; set ; }
public UserProfile UserProfiles { get ; set ; }

public int PostId {获得; set ; }
public virtual 帖子帖子{ get < /跨度>; set ; }

// public virtual ICollection< UserComment> UserComments {get;组; }

public int UserCommentId {获得; set ; }
public virtual UserComment Usercomments { get < /跨度>; set ; }

}
}





我想在一个视图中使用2个模型。我使用了部分视图,但评论占据了最高位置,而报价占据了较低的位置。因为部分视图是这样的:



 {
@ Html.Action( _ Comment 论坛 new {id = @ Model.PostId})

@ Html.Action( _ Quote 论坛 new {id = Model.PostId})

}







请问我该如何完成?

解决方案

你没有。您只能将单个模型传递给视图。



您可以使用多个模型,只需将所有要传递的模型包装在另一个包含的模型中即可它们。

I have my Comment model like this:

{
    public class UserComment
    {
        public UserComment()
        {
            DateUserCommented = DateTime.Now;
        }
        public int UserCommentId { get; set; }

        [Required(ErrorMessage = "Field is Required")]
        [Display(Name = "Comment Message")]
        public string UserCommentSubject { get; set; }

        [Display(Name = "Commented By")]
        public string UserCommentedBy { get; set; }

        public string UserCommentFile { get; set; }

        [Display(Name = "Date Commented")]
        public DateTime DateUserCommented { get; set; }

        public int UserId { get; set; }
        public UserProfile UserProfiles { get; set; }

        public int PostId { get; set; }
        public virtual Post Posts { get; set; }

        //public int QuoteId { get; set; }
        //public virtual Quote Quotes { get; set; }

        public virtual ICollection<Quote> Quotes { get; set; }
        
    }
}



And my Quote model like this:


{
    public class Quote
    {
        public Quote()
        {
            DateQuoted = DateTime.Now;
        }
        public int QuoteId { get; set; }

        [Required]
        public string QuoteContent { get; set; }

        [Display(Name = "Replied By")]
        public string QuotedBy { get; set; }

        [Display(Name = "Date Quoted")]
        public DateTime DateQuoted { get; set; }

        public string QuoteFile { get; set; }

        public int UserId { get; set; }
        public UserProfile UserProfiles { get; set; }

        public int PostId { get; set; }
        public virtual Post Posts { get; set; }

        //public virtual ICollection<UserComment> UserComments { get; set; }

        public int UserCommentId { get; set; }
        public virtual UserComment Usercomments { get; set; }

    }
}



I want to use the 2 models in one view. I have used a Partialview, but the comment takes the top position and the Quote takes the lower position. because the partial view is like this:

{
@Html.Action("_Comment", "forum", new { id = @Model.PostId })

    @Html.Action("_Quote", "forum", new { id = Model.PostId })

}




Please how do i get it done??

解决方案

You don't. You can only ever pass a single model to the View.

You can use multiple models though simply by wrapping all of the models you want to pass in another class that contains them.


这篇关于请问如何在视图中使用2个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆