我很難讓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; }
}
}多大的幫助
感謝。