2017-04-06 86 views
1

我很難讓MVC綁定到我創建的模型。過去我已經成功完成了好幾次。因此,我只是不確定爲什麼它不在這個項目中工作。C#模型綁定到自定義類不工作

例如,我有以下觀點:

@model StoryWall.ViewModels.ViewPostViewModel 
@{ 
    ViewBag.Title = "View Post"; 
} 

<article class="story"> 
    <header> 
     <h1>@Model.story.Title</h1> 
     <spann class="text-muted">@Model.story.Store.StoreName</span> 
      <h2>Posted by @Model.story.PosterName</h2> 
</header> 

    if(@Model.story.StoryImage != null) { 
    <div class="storyImageWrapper"> 
     <img src="~/img/@Model.story.StoryImage" /> 
    </div> 

    <p>@Model.story.StoryBody</p> 
    } 
</article> 

<div class="commentsSection"> 
    <h2>Comments</h2> 
    <h3>Add a Comment</h3> 
    <form method="post" class="form-horizontal" name="CommentForm" action="/View/AddComment"> 
     @Html.AntiForgeryToken() 
     <input type="hidden" name="newComment.StoryID" value="@Model.story.StoryID" /> 
     <div class="form-group"><label class="control-label col-sm-2">Name </label><div class="col-sm-10">@Html.TextBoxFor(@m => m.newComment.CommenterName, new { @class = "form-control", @required = true, @ng_model = "CommenterName"}) <span class="text-warning" ng-show="CommentForm.newComment.CommenterName.$dirty && CommentForm.newComment.CommenterName.$invalid"> Required </span> <span class="text-warning"> @Html.ValidationMessageFor(@m => m.newComment.CommenterName) </span></div> </div> 
     <div class="form-group"><label class="control-label col-sm-2">Email </label><div class="col-sm-10">@Html.TextBoxFor(@m => m.newComment.CommenterEmail, new { @class = "form-control", @required = true, @ng_model = "CommenterEmail" }) <span class="text-warning" ng-show="CommentForm.newComment.CommenterEmail.$dirty && CommentForm.newComment.CommenterEmail.$invalid"> Required </span> <span class="text-warning"> @Html.ValidationMessageFor(@m => m.newComment.CommenterEmail) </span></div> </div> 
     <div class="form-group"><label class="control-label col-sm-2">Message </label><div class="col-sm-10">@Html.TextAreaFor(@m => m.newComment.CommentBody, new { @class = "form-control", @required = true, @ng_model = "CommentBody" }) <span class="text-warning" ng-show="CommentForm.newComment.CommentBody.$dirty && CommentForm.newComment.CommentBody.$invalid"> Required </span> <span class="text-warning"> @Html.ValidationMessageFor(@m => m.newComment.CommentBody) </span></div> </div> 
     <button type="submit" ng-disabled="CommentForm.$invalid">Submit</button> 
    </form> 
    <h3>Current Comments</h3> 
    @foreach(var comment in @Model.story.Comments) { 
     <blockquote>@comment.CommentBody</blockquote> 
    <span>Poster: @comment.CommenterName on @comment.DatePosted.ToString("MM-dd-yyyy")</span> 

    } 

</div> 

即使我特別使用Html.TextBoxFor()對我的輸入框,綁定仍無法按預期工作。

這是我的控制器。第二個Action方法中的「註釋」沒有正確綁定;其屬性爲空。

public class ViewController : Controller 
{ 

    StoryModel dbContext = new StoryModel(); 

    public ActionResult ViewPost(Int32 postID) 
    { 
     ViewPostViewModel vm = new ViewPostViewModel(); 
     vm.story = dbContext.Stories.FirstOrDefault(s => s.StoryID == postID); 
     return View(vm); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult AddComment(Comment comment) 
    { 

     if (ModelState.IsValid) 
     { 
      dbContext.Comments.Add(comment); 
      dbContext.SaveChanges(); 
      return RedirectToAction("ViewPost", new { storyID = comment.StoryID}); 
     } 
     else 
     { 
      ViewPostViewModel vm = new ViewPostViewModel(); 
      vm.story = dbContext.Stories.FirstOrDefault(s => s.StoryID == comment.StoryID); 
      vm.newComment = comment; 
      return View("ViewPost", vm); 
     } 
    } 
} 

我知道這是不是第一次了類似的問題已經被問過,但我無法找到解決我的問題的解決方案。另外,如前所述,這是我過去成功所做的事情。

對我而言,這個場景中唯一的「新」元素是Angular.js。這是我第一次使用該框架。莫名其妙地干涉綁定?

如果有幫助,註釋型號:

public partial class Comment 
{ 
    public int CommentID { get; set; } 

    public int? StoryID { get; set; } 

    public int UserID { get; set; } 

    [Required] 
    [StringLength(75)] 
    public string CommenterName { get; set; } 

    [Required] 
    [StringLength(75)] 
    public string CommenterEmail { get; set; } 

    [Required] 
    public DateTime DatePosted { get; set; } 

    [Required] 
    public string CommentBody { get; set; } 


    public virtual Story Story { get; set; } 

    public virtual User User { get; set; } 
} 

和ViewPostViewModel

public class ViewPostViewModel 
{ 

    public Story story { get; set; } 
    public Comment newComment { get; set; } 
} 

}多大的幫助

感謝。

回答

1

答案之一是使用@ Html.EditorFor()

@Html.EditorFor(m => m.newComment) 

然後在視圖放在你創建一個名爲與被準確命名爲視圖EditorTemplates新文件夾中的文件夾對象類型。在這種情況下,Comment.cshtml

的觀點可能是這樣的 - >

​​

這種做法是一個我通常使用與清單的工作(有用的調查或測試),但它也適用於單個項目。

另一種方法可能是將所有內容都添加到視圖模型中,因爲視圖模型不需要是業務對象或數據庫模型的一對一映射。 :)

編輯:忘了補充。我認爲使用這種方法接收帖子的方法將不得不接收整個ViewModel,而不僅僅是評論。 - >

public ActionResult AddComment(ViewPostViewModel vm)