我有一個非常簡單的局部視圖,我使用ajaxform。在我的部分視圖中,我有一個單獨的textArea和一個Submit按鈕。 問題是無論我寫入文本區域,它都不會將數據發送到控制器,而是發送一個文本=「註釋」。 如果我不寫任何東西,驗證完美。查看不發送正確的數據到控制器
視圖模型:
public class NoteCommentViewModel
{
public Int32 Id { get; set; }
[Required(ErrorMessage="Hey, if you dont wanna write then why pressing the button !!")]
public string Comment { get; set; }
public DateTime CommentDate { get; set; }
public long UserId { get; set; }
public double Rating { get; set; }
public Guid NoteId { get; set; }
}
控制器:
//
//GET:/Notes/Comment/
public ActionResult Comment(string id)
{
ViewBag.NoteId = id;
var m = new NoteCommentViewModel()
{
NoteId = new Guid(id),
UserId = Convert.ToInt64(Session["LoginUserId"].ToString()),
//Comment=""
};
return PartialView(m);
}
//
//POST:/Notes/Comment
[HttpPost]
public ActionResult Comment(NoteCommentViewModel nvm)
{
NoteRatingComments comment = new NoteRatingComments();
comment.Comment = nvm.Comment; // Here I always have "Comment", regardless whatever I write in the page.
comment.EntryDate = DateTime.Now;
comment.NoteId = nvm.NoteId;
comment.UserId = nvm.UserId;
comment.Rating = 3.00;
dc.NoteRatingComments.AddObject(comment);
dc.SaveChanges();
return Content(Boolean.TrueString);
}
的觀點:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Ajax.BeginForm("Comment", "Notes", null, new AjaxOptions
{
UpdateTargetId = "Comment-message",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
OnSuccess = "commentSuccess"
}, new { id = "commentForm" }))
{
<div style="margin-top:20px;">
<div id="commentSuccess"></div>
<div class="comentPic">
@{
long UserId = Convert.ToInt64(Session["LoginUserId"].ToString());
string Fullname = Session["LoginUserFullName"].ToString();
}
<img src='https://graph.facebook.com/@UserId/picture' height="100px" width="100px"/>
</div>
<div class="commentText">
@Html.HiddenFor(m => m.UserId)
@Html.HiddenFor(m=>m.NoteId)
@Html.TextAreaFor(m => m.Comment, new { style = "width:600px;height:120px;" })
<br />
@Html.ValidationMessageFor(m => m.Comment)
<div style="text-align:right;">
<input type="submit" value="Comment" name="comment" class="btn"/>
</div>
</div>
<div class="clear"></div>
</div>
}
以下是錯誤...的屏幕截圖,以便更好地理解。我在視圖中寫入「數據」,但在控制器中我收到「註釋」。它來自哪裏?
將是巨大的,如果有人可以幫我找出問題...!
heyy ...這就是超級酷。解決了我的問題,我也學到了一件好事。感謝m8。 – kandroid