Robert Young
Robert Young

Reputation: 171

Passing multiple values into a view

I have a view that I want to take in a thread and a comment so that way when I do the post it will have both and be able to update both.

Here is the view:

@model GameDiscussionBazzar.Data.Comment
 @{
    ViewBag.Title = "EditComment";
    Layout = "~/Views/Shared/_EditCommentLayout.cshtml";
}
<div class="EditComment">
<h1>
    Edit Comment
</h1>
@using (Html.BeginForm("EditThreadComment", "Comment"))
{
    <div class="EditingComment">
        @Html.EditorForModel()
        @Html.Hidden("comment", Model)
        @Html.HiddenFor(i => i.ParentThread)
        <input type="submit" value="Save"/>
        @Html.ActionLink("Return without saving", "Index")

    </div>
}
</div>

I have 2 methods one is just an action result and returns the view while the other is the post that returns to the main page if it succeded. here are the methods:

public ActionResult EditThreadComment(int commentId)
    {
        Comment comment = _repository.Comments.FirstOrDefault(c => c.Id == commentId);

        return View(comment);
    }
    [HttpPost]
    public ActionResult EditThreadComment(Comment comment, Thread thread)
    {
        var c = thread.ChildComments.FirstOrDefault(x => x.Id == comment.Id);
        thread.ChildComments.Remove(c);
        if (ModelState.IsValid)
        {
            _repository.SaveComment(comment);
            thread.ChildComments.Add(comment);
            _tRepo.SaveThread(thread);
            TempData["Message"] = "Your comment has been saved";
            return RedirectToAction("Index", "Thread");
        }
        else
        {
            TempData["Message"] = "Your comment has not been saved";
            return RedirectToAction("Index", "Thread");
        }
    }

So again my question is How do I pass 2 parameters into the view? Or how do I pass the values of my thread?

Upvotes: 1

Views: 4911

Answers (3)

JasCav
JasCav

Reputation: 34632

In order to pass back multiple values, you should create a ViewModel which can hold whatever objects and values that you wish to change and (ultimately) pass it back to the view.

So, create a new model like this...(I'm not at a compiler right now, so I apologize if some of this code doesn't build).

public class PostViewModel
{
    public Comment Comment { get; set; }
    public Thread Thread { get; set; }
}

In your controller, you need to basically convert back and forth between your PostViewModel.

public ActionResult EditThreadComment(int commentId)
{
    PostViewModel post = new PostViewModel();

    Comment comment = _repository.Comments.FirstOrDefault(c => c.Id == commentId);
    post.Comment = comment;
    post.Thread = new Thread();

    return View(post);
}

public ActionResult EditThreadComment(PostViewModel post)
{
    Comment comment = post.Comment;
    Thread thread = post.Thread;

    // Now you can do what you need to do as normal with comments and threads
    // per the code in your original post.
}

And, in your view, you will now have it strongly typed to a PostViewModel now. So, at the top...

@model GameDiscussionBazzar.Data.PostViewModel

And you'll have to just go one level deeper to get to the individual Comment and Thread objects.

Upvotes: 4

AdaTheDev
AdaTheDev

Reputation: 147234

You can create a ViewModel class to hold a Comment and a Thread, then pass that single view model to the view which can then access both the Comment and the Thread within it.

Upvotes: 1

pollirrata
pollirrata

Reputation: 5286

You can use ViewBag.Thread = myThread

Upvotes: 0

Related Questions