2012-03-08 5 views
1

게시글을 게시 할 때 게시글을 게시 할 때 주석을 가져 가고 싶기 때문에 게시를 할 때 두 가지를 모두 가지고 업데이트 할 수 있습니다. 내가 하나가 바로 작업 결과이고, 다른 하나는이 succeded 경우 메인 페이지로 돌아 포스트 동안 뷰를 돌려줍니다이 개 방법이보기에 여러 값 전달하기

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

: 여기

뷰입니다. 여기 방법은 다음과 같습니다

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

그래서 다시는 내 문제는 내가보기에이 개 매개 변수를 전달하려면 어떻게해야입니까? 아니면 내 스레드의 값을 전달합니까?

답변

0

당신은 ViewBag.Thread은 = myThread

당신은 코멘트와 스레드를 보유 할 뷰 모델 클래스를 생성 할 수 있습니다
1

, 그럼 다음 코멘트와 스레드 내에서 모두 액세스 할 수있는보기에 그 단일 뷰 모델을 통과 사용할 수 있습니다 그것.

4

여러 값을 다시 전달하려면 변경하려는 개체 및 값을 보유 할 수있는 ViewModel을 만들어야하며 (궁극적으로)이를 다시보기로 전달할 수 있습니다.

이렇게 새 모델을 만드십시오. (저는 지금 컴파일러가 아니기 때문에이 코드가 빌드되지 않으면 사과드립니다.)

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

컨트롤러에서 기본적으로 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. 
} 

이제보기에서 이제 PostViewModel에 강력하게 입력하게됩니다. 그래서, 상단에 ...

@model GameDiscussionBazzar.Data.PostViewModel 

그리고 당신이해야 그냥 개인 의견 및 스레드 개체를 얻기 위해 한 단계 더 깊이 이동합니다.