【问题标题】:How to make the model of the partial view independent from the view itself?如何让局部视图的模型独立于视图本身?
【发布时间】:2019-02-04 11:39:31
【问题描述】:

首先请不要将此问题标记为重复,因为我已经检查过这些问题:

pass a different model to the partial view

MVC PartialView in multiple Views with different models

MVC 4 - Use a different model in partial view

但在我的情况下,它们都没有帮助。

我正在开发一个 Asp.NET MVC 新闻网站。 我的主页称为提要,而我的部分视图称为 _Comments。 _Comment 部分视图有一个文本区域和一个提交按钮,用于根据点击的提要保存 cmets。

_评论部分观点:

    <div class="col-sm-12">
    @using (Ajax.BeginForm("AddComment", "Home", new AjaxOptions() 
    {UpdateTargetId= "update-comment", InsertionMode = InsertionMode.Replace }))
    {
     <textarea type="text" class="float-none form-control" required 
    name="comment" row="6" placeholder="What do you think?"></textarea>
    <br />
    <button type="submit" class="float-right btn btn-primary" 
    name="feedID" value="@Session["FeedID"]">Add comment</button>
    }
 </div>

我就是这样在 feed 页面中调用局部视图的。

<div class="col-sm-12 col-md-12 col-lg-12 col-xl-12">
@Html.Partial("_Comment")
</div>

我打算在 _comment 部分视图下显示添加的 cmets,在另一个名为“_AddedComments”的部分视图中

_添加评论

@model News.CommentViewModel
@if (Model.CommentV != null)
{
    foreach (var item in Model.CommentV)
    {
        @item.Comment_Text <br />
    }
}  

所以在我的提要视图中,我将两个部分视图称为这样

<div class="col-sm-12 col-md-12 col-lg-12 col-xl-12">
            @Html.Partial("_Comment")
 </div>
 <div class="col-sm-12" id="update-comment">
        @Html.Partial("_AddedComments")
 </div>

我有一个名为 Comments 的表格,其中包含:

评论ID

评论文本

Feed_ID_FK

现在,当我在 _comment Partial 视图中按下添加评论时,它会将我带到这个控制器方法

  public PartialViewResult AddComment(string comment, int feedID)
    {
        Comment newComment       = new Comment();
        newComment.Feed_FK_ID    = feedID;
        newComment.Comment_Text  = comment;
        DB.Comments.Add(newComment);
        DB.SaveChanges();
        List<Comment> myComments = DB.Comments.Where(a => a.Feed_FK_ID == 
         feedID).ToList();

        CommentViewModel viewModel = new CommentViewModel
        {
            CommentV = myComments
        };  

        return PartialView("_AddedComments");
        }

我的 CommentViewModel 包含

  public List<Comment> CommentV { get; set; }  // Comments table
  public List<Feed> FeedV { get; set; }        // Feeds Table

提要页面中的“myViewModel”几乎包含所有内容(新闻类别、提要、用户、图像...)

所以在提要页面的最顶部,我调用了 MyviewModel

@model News.myViewModel

在 _AddedComments 我调用 CommentsViewModel

@model News.CommentViewModel.

问题是每次我打开提要页面时都会收到以下消息: “传递到字典中的模型项的类型为‘News.myViewModel’,但此字典需要‘News.CommentViewModel’类型的模型项” 即使我在部分视图中传递了 CommentViewModel。 任何帮助将不胜感激。

我尝试像这样调用 _AddedComments: @Html.Partial("_AddedComments",new CommentViewModel()) 但我有一个空异常。 我完成这项任务的唯一方法是在原始视图模型中添加一个评论列表,但我想这不是一个好习惯,因为模型“myViewModel”包含 7 个表,我只想传递 cmets 表所以我猜传递模型“MyViewMdoel”不是一个好主意。

【问题讨论】:

  • @Html.Partial( 渲染局部视图而不调用局部视图的服务器端操作方法。如果你想渲染局部视图_AddedComments_Comment并执行它们各自的动作,你应该使用@{Html.RenderAction(&lt;ActionName&gt;);}
  • 感谢您的回复,但我现在进入剃刀视图“无法将类型 'void' 隐式转换为 'object”

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


【解决方案1】:

如下更改您的代码,

public ActionResult AddComment(string comment, int feedID)
    {
        Comment newComment       = new Comment();
        newComment.Feed_FK_ID    = feedID;
        newComment.Comment_Text  = comment;
        DB.Comments.Add(newComment);
        DB.SaveChanges();
        List<Comment> myComments = DB.Comments.Where(a => a.Feed_FK_ID == 
         feedID).ToList();

        CommentViewModel viewModel = new CommentViewModel
        {
            CommentV = myComments
        };  

        return PartialView("_AddedComments",viewModel );
        }

【讨论】:

猜你喜欢
  • 2020-01-04
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
相关资源
最近更新 更多