0

First please do not mark this question as duplicate because I've already checked these:

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

but none of them helped in my case.

I'm working on a Asp.NET MVC news website. my home page is called feed,and my partial view is called _Comments. the _Comment partial view has a text area and a submit button to save the comments depending on the clicked feed.

_Comment partial view :

    <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>

I'm calling the partial view in the feed page in this way.

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

And I'm planning to show the added comments underneath the The _comment partial view, in another partial View Called "_AddedComments"

_AddedComments

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

so in my feed view I'm calling the 2 partial views like this

<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>

I have a table called Comments that contains:

CommentID

CommentText

Feed_ID_FK

Now when I press the add comment in the _comment Partial view it takes me to this controller method

  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");
        }

My CommentViewModel contains

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

and "myViewModel" in the feeds page contains almost everything (News categories,feeds,users,images...)

So in very top of the feed page I'm calling MyviewModel

@model News.myViewModel

and in the _AddedComments I'm calling CommentsViewModel

@model News.CommentViewModel.

The problem is every time i open up the feeds page I'm getting this message : " The model item passed into the dictionary is of type 'News.myViewModel', but this dictionary requires a model item of type 'News.CommentViewModel' " even though I'm passing the CommentViewModel in the partial View. Any help would be so appreciated .

I tried to call the _AddedComments like this: @Html.Partial("_AddedComments",new CommentViewModel()) but I got a null exception. The only way I got this task working is by adding a comment list into the original View model, but I guess it's not a good practice because the model " myViewModel" contains like 7 tables and I just want to pass the comments table so I guess it's not a good idea to pass the model "MyViewMdoel".

Alaa Hershey
  • 85
  • 1
  • 9
  • `@Html.Partial(` renders the partial view without calling the server side action method of the partial view. If you want to render the partial view `_AddedComments` and `_Comment` with their respective actions executed, you should use `@{Html.RenderAction();}` – Chetan Feb 04 '19 at 11:53
  • Thanks for replying,but i'm getting now in the razor view "Cannot implicitly convert type 'void' to 'object " – Alaa Hershey Feb 04 '19 at 12:06
  • Please refer answer of https://stackoverflow.com/questions/14157072/using-renderactionactionname-values-in-mvc4-issue – Chetan Feb 04 '19 at 12:08

1 Answers1

1

change your code as below,

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 );
        }
Anoos
  • 186
  • 7
  • i just did that and edit the calling of the partial view by using @{Html.RenderAction();} ,but now the partial view open in a new page. I checked the UnobtrusiveJavaScriptEnabled" value="true" , and I've already added http://ajax.aspnetcdn.com/ajax/mvc/5.0/jquery.validate.unobtrusive.js – Alaa Hershey Feb 04 '19 at 12:48