So I have a view called index that lays out all of the threads in my database. Then inside that view I'm loading all the comments on the threads. When I call on my form that is supposed to create a new comment it keeps telling me that my model state is invalid. It tells me that it cannot convert from type string to type profile or comment or tag. Originally I had this as my code:
public ActionResult AddComment(Thread thread, string commentBody)
{
if (ModelState.IsValid)
{
_repository.AddComment(thread, comment);
TempData["Message"] = "Your comment was added.";
return RedirectToAction("Index");
}
Then I changed it to this:
public ActionResult AddComment(Thread thread, string commentBody)
{
Profile profile = _profileRepository.Profiles.FirstOrDefault(x => x.Id == thread.ProfileId);
Tag tag = _tagRepository.Tags.FirstOrDefault(t => t.Id == thread.TagId);
thread.ThreadTag = tag;
thread.Profile = profile;
Comment comment = new Comment()
{
CommentBody = commentBody,
ParentThread = thread
};
if (ModelState.IsValid)
{
_repository.AddComment(thread, comment);
TempData["Message"] = "Your comment was added.";
return RedirectToAction("Index");
}
This still tells me that my model state is invalid. How do I get it so that it will not try to change the state?
Also here is the form that is being used to call this action:
@using(Html.BeginForm("AddComment", "Thread", mod))
{
<input type="text" name="AddComment" id="text" />
<input type="submit" value="Save"/>
}
In the instance of code above mod is the model which is a thread. And as requested here is everything inside of thread:
public Thread()
{
this.ChildComments = new HashSet<Comment>();
}
public int Id { get; set; }
public string TopicHeader { get; set; }
public string TopicBody { get; set; }
public Nullable<int> UpVotes { get; set; }
public Nullable<int> DownVotes { get; set; }
public int ProfileId { get; set; }
public int TagId { get; set; }
public virtual Profile Profile { get; set; }
public virtual ICollection<Comment> ChildComments { get; set; }
public virtual Tag ThreadTag { get; set; }
And finally the comment class:
public partial class Comment
{
public int Id { get; set; }
public string CommentBody { get; set; }
public int UpVotes { get; set; }
public int DownVotes { get; set; }
public virtual Thread ParentThread { get; set; }
}