2017-06-22 16 views
0

私は非常に簡単なブログを構築しています。私のブログでは、コメントとメッセージという2つのモデルが必要です。あなたのビューに2つのモデルを使用するには、ViewModelが必要ですが、かなりの困難があります。何らかの理由で、私は両方のモデルにアクセスすることができません。同じことが私の部分的な見解です。今私は非常にasp.net mvcの新人であり、それは愚かな間違いがpropably愚かなことを知っているが、私は誰かが私が間違って、最も重要な理由を示すことができることを願っています!ここでViewModelからViewおよびPartialViewにデータを渡す際の問題

ここ

namespace Portfolio.Models 
{ 
    public class Messages 
    { 
     public int MessagesId { get; set; } 
     [Required] 
     public string Title { get; set; } 
     [Required] 
     public string Body { get; set; } 
     public DateTime WhenCreated { get; set; } 

     public Messages() 
     { 
      WhenCreated = DateTime.Now; 
     } 
    } 
} 

namespace Portfolio.Models 
{ 
    public class Comments 
    { 
     public int CommentsId { get; set; } 
     public string Comments_body { get; set; } 
     public Messages MessagesId { get; set; } 
     public DateTime WhenCreated { get; set; } 

     public Comments() 
     { 
      WhenCreated = DateTime.Now; 
     } 
    } 
} 

が私のViewModel

namespace Portfolio.ViewModels 
{ 
    public class MessageViewModel 
    { 
     public IEnumerable<Messages> Messages { get; set; } 
     public IEnumerable<Comments> Comments { get; set; } 
    } 
} 

私のコントローラ

namespace Portfolio.Controllers 
{ 
    public class MessagesController : Controller 
    { 
     private ApplicationDbContext _context; 

     public MessagesController() 
     { 
      _context = new ApplicationDbContext(); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      _context.Dispose(); 
     } 
     public ActionResult Blog() 
     { 
      var messages = _context.messages.ToList(); 

      return View(_context.messages.OrderByDescending(Messages => 
      Messages.WhenCreated)); 
     } 

     public ActionResult Comment() 
     { 
      var comment = _context.comments.ToList(); 

      return View(_context.comments.OrderByDescending(Comments => 
      Comments.WhenCreated)); 
     } 
     public ActionResult Post() 
     { 
      return View(); 
     } 


     //This binds the objects from the database to the values from the 
     view 

     [ValidateInput(false)] 
     [HttpPost] 
     public ActionResult Create(FormCollection formValues) 
     { 
      try 
      { 
       Messages message = new Messages(); 
       message.Title = formValues["Title"]; 
       message.Body = formValues["editor"]; 

       Comments comment = new Comments(); 
       comment.Comments_body = formValues["editor" + 
       message.MessagesId]; 

       _context.comments.Add(comment); 
       _context.SaveChanges(); 
       _context.messages.Add(message); 
       _context.SaveChanges(); 

      } 
      catch 
      { 
       return View(); 
      } 
      return RedirectToAction("Blog"); 
     } 
    } 
} 

である私のモデルですは、ここに私のビュー

@model IEnumerable<Portfolio.ViewModels.MessageViewModel> 

@{ 
    ViewBag.Title = "Blog"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
<link rel="stylesheet" type="text/css" href="~/Content/css/Blog.css" /> 
<script src="~/Scripts/ckeditor/ckeditor.js"></script> 

<div class="jumbotron opacity_container"> 
    <div class="row"> 
     <div class="col-md-12"> 
      <h2>Latest Posts</h2> 
      <div class="row"> 
       <div class="col-md-12"> 
        @foreach (var messages in Model) 
        { 
         <div class="jumbotron opacity_container"> 
          <div class="col-md-12"> 
           <div class="panel panel-primary"> 
            <div class="panel-heading"> 
             @*Gets the title of the blog post*@ 
             <h2 class="panel-title"> 
             @messages.Title 
             </h2>@messages.WhenCreated 
            </div> 
@*Gets the body of the blog post and decodes the html of the ckeditor*@ 

           <div class="panel-body"> 
       @Html.Raw(System.Web.HttpUtility.HtmlDecode(messages.Body)) 
           </div> 
          </div> 
         </div> 
@*this button gets the id from the database of the 
Message table this helps to prevent that all the comments from all blogs 
gets shown and thus shows only the comments that belong to the blog in 
question*@ 

        <button class="btn btn primary"id="@messages.MessagesId" 
        onclick="ShowComments(this.id)"> 
        Show Comments 
        </button> 

@*this is the container where al the comments are placed in and where you 
can post comments. The comments are placed in the Comment partial view*@ 
         <div class="hidden" id="[email protected](messages.MessagesId)"> 
          @Html.Partial("_Comment", Model) 

@*this button gets the id from the database of the Message table this helps 
to prevent that all the comments from all blogs gets hidden and thus 
hides only the comments that belong to the blog in question*@ 
         <button class="btn btn-primary" 
         id="@messages.MessagesId" 
         onclick="HideComments(this.id)">Hide Comments 
         </button> 
        </div> 
       </div> 
       } 
      </div> 
     </div> 
    </div> 
</div> 

これは私の部分図

@{ 
    ViewBag.Title = "Home Page"; 
} 
<link rel="stylesheet" type="text/css" href="~/Content/css/Blog.css" /> 

<div class="row" id="CommentContainer"> 
    <div class="col-md-12"> 
     <h3>Post Comment</h3> 
@*The form to post comments*@ 
     @using (Html.BeginForm("Create", "Messages")) 
      { 
      <div class="form-group"> 
       <label>Comment</label> 
       @Html.TextArea("editor1", htmlAttributes: new { name = 
       "editor1", id = "editor", rows = "10", cols = "180" }) 
      </div> 
       <button type="submit" class="btn btn-primary" 
       id="PostButton">Post Comment</button> 
      } 

@*CKEdito script*@ 
     <script> 
      CKEDITOR.replace('editor1'); 
     </script> 


     <div class="row"> 
      <div class="col-md-10"> 
@*Places al the comments and decodes the html from the ckeditor*@ 
       @foreach(var comments in Model) 
       { 
        <div class="well" id="CommentBox"> 

@Html.Raw(System.Web.HttpUtility.HtmlDecode(comments.Comments_body)) 
        </div> 
       } 
      </div> 
     </div> 
    </div> 
</div> 

Viewはブログのメッセージを示しているが、このPartialViewがコメントを示しているです。 メッセージモデルからビューにデータを送信する必要があり、部分モデルでコメントモデルからデータを送信および取得する必要があります。

ヘルプを評価してください!

+0

現在、何らかのエラーメッセージが表示されていますか?もしあなたがそれを共有することができますか? – Hopeless

+0

これは私が受け取るエラーメッセージですMessageViewModel '' Title 'の定義がなく、' MessageViewModel 'タイプの最初の引数を受け入れる拡張メソッド' Title 'が見つかりませんでした(usingディレクティブまたはアセンブリ参照がありませんか?) – ruinerwarrior

+0

ビューのモデルプロパティをコレクションViewModelクラスに設定しましたが、ビューのコレクションまたはコメントのコレクションを送信しています –

答えて

0

ビューでViewModelを使用するには、そのビューをViewからビューに渡す必要があります。現在、メッセージのコレクションに渡している:

public ActionResult Blog() 
    { 
     var messages = _context.messages.ToList(); 

     // Your view is expecting a parameter of type MessageViewModel, 
     // but you're passing it an object of type List<Messages> 
     return View(_context.messages.OrderByDescending(Messages => 
     Messages.WhenCreated)); 
    } 

あなたのViewModelを使用する場合のようなものを試してみてください。また、あなたのビューを更新する必要があります

 public ActionResult Blog() 
    { 
     //Create a MessageViewModel and assign its properties... 
     var viewModel = new MessageViewModel() 
     { 
      Messages = _context.messages.OrderByDescending(m => m.WhenCreated), 
      Comments = _context.comments 
     }; 

     // Pass the MessageViewModel to your view. 
     return View(viewModel); 
    } 

@model Portfolio.ViewModels.MessageViewModel 
... 
@foreach (var messages in Model.Messages) 
... 
@Html.Partial("_Comment", Model.Comments) 

そして、あなたの部分的なビューには IEnumberable<Comments>のパラメータをモデルにしておきたいと思うでしょう:

@model IEnumerable<Portfolio.Models.Comments> 

は、このソリューションは、現在すべてのメッセージと_contextからすべてコメントをつかんでビューに渡すされることに注意してください。おそらくそれらをフィルタリングしたいでしょう(特定のメッセージに添付されたコメントなどを渡すだけです)。

+0

提案を適用しましたが、同じエラーが表示されます。私はコントローラをビューではなく変更しただけなので、ビューで間違って呼んでいる可能性があります。 – ruinerwarrior

+0

私はちょうどビューと部分的なビューにいくつかの変更を含めるように答えを更新しました。 –

+0

thnxすぐにそれを試してみよう。私は非常に新しい両方のC#とasp.netので本当にありがとうヘルプ – ruinerwarrior

関連する問題