2017-04-06 74 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)