Johnson Duru
Johnson Duru

Reputation: 682

How to add comment to mvc application

I have a list of comments under my detail page in MVC. I want the user to be able to add comments in the same page and save to the database. How do I pass data to the controller action and save this using dbcontext that holds my comment class. I have this follow code in my CommentController:

 public ActionResult Create(int MovieId)
     {
         var moviecomment = _db.Moviecomment.Where(r => r.MovieID == MovieId);


         return View(moviecomment);

     }
     [HttpPost]
     public ActionResult Create(MovieComment Moviecomment)
     {
         var MovieComment = _db.Moviecomment.Add(Moviecomment);


         _db.SaveChanges();

         return RedirectToAction("Details");
     }

And has a partial View:

 @model MvcMovie.Models.MovieComment

 @using (Html.BeginForm())
    {            
        @Html.ValidationSummary(true)
        <div class="addcommentbox">
            <h2> Add Comment </h2>
            @Html.TextAreaFor(model => model.comment)
            <div class="ErrorMessage">
            @Html.ValidationMessageFor(model => model.comment)
            </div>                    
            <input id="addComment" type="button" onclick="" value="Add" />
        </div>

    }   

and in my Detail page i have this:

@model MvcMovie.Models.Movie

@{
ViewBag.Title = "Details";
 }

<h2>Details</h2>
Movie Title @Html.DisplayFor(model => model.Title) Genre @Html.DisplayFor(model => model.Genre) PostDate @Html.DisplayFor(model => model.PostDate) Staring @Html.DisplayFor(model => model.Staring) Description @Html.DisplayFor(model => model.Description) Trailer @Html.DisplayFor(model => model.Trailer)
<fieldset>
        @Html.Partial("Comments",Model.MovieComment)
</fieldset>


<p>


</p>
<p>
 @Html.ActionLink("Edit", "Edit", new { id=Model.MovieID }) |
  @Html.ActionLink("Back to List", "Index")
 </p> 
 @Html.RenderAction("Create","Comment" new { id = Model.MovieID })

i want user to able to add comment when they are in detail page: other thing seem to been fine but this line

 @Html.RenderAction("Create","Comment" new { id = Model.MovieID    })   

give me the following error. cannot implicitly convert type void to object. Please any help will be appreciated.

The model code: public class Movie {

    public int MovieID { get; set; }
    public string Title { get; set; }
    public String Genre { get; set; }
    public DateTime PostDate { get; set; }

    public string Staring { get; set; }
    public string Description { get; set; }
    public string Picture { get; set; }
    public string Trailer { get; set; }


    public virtual ICollection< MovieComment> MovieComment { get; set; }

}

For comment

public class MovieComment
 {
 // public MovieComment()
 //{
 //    Movie = new HashSet<Movie>();
 //}


        public int MovieCommentID { get; set; }
        public string comment_title { get; set; }
        public String comment { get; set; }
        public int MovieID { get; set; }

        // [ForeignKey("ID")]
        public virtual Movie Movie { get; set; }

        //[ForeignKey("ProfileID")]

        public virtual Profile Profile { get; set; }
        public String ProfileID { get; set; }
        //public string MovieUserID { get; set; }

}

Upvotes: 0

Views: 6620

Answers (1)

Giscard Biamby
Giscard Biamby

Reputation: 4619

Not sure if this is your only error, but the syntax is wrong for the line that's giving you an error. You are missing a comma. Try:

@Html.RenderAction("Create","Comment", new { id = Model.MovieID    })

If this doesn't fix your problem, can you also post the code for your model?

Upvotes: 0

Related Questions