2012-06-09 157 views
0

我有一個非常簡單的局部視圖,我使用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> 
} 

以下是錯誤...的屏幕截圖,以便更好地理解。我在視圖中寫入「數據」,但在控制器中我收到「註釋」。它來自哪裏?

enter image description here

enter image description here 將是巨大的,如果有人可以幫我找出問題...!

回答

1

問題是您的提交按鈕的name屬性與Comment textarea name屬性相同。

要解決這一點,你需要更改提交按鈕的name比「註釋」,或從提交按鈕刪除name屬性,所以更改別的東西:

<input type="submit" value="Comment" name="comment" class="btn"/> 

<input type="submit" value="Comment" class="btn"/> 

因爲Ajax.BeginForm使用jQuery .serializeArray()方法 - 因爲您的提交按鈕有一個name,並且此輸入觸發提交 - 也發送提交按鈕的值"Comment"到服務器。

+0

heyy ...這就是超級酷。解決了我的問題,我也學到了一件好事。感謝m8。 – kandroid

1

我不確定你的問題到底是什麼。但下面的代碼應該可以工作。

public class NoteCommentViewModel 
{ 
    public Int32 Id { get; set; } 

    [Required(ErrorMessage=" Your Error message")] 
    [DataType(DataType.MultilineText)] 
    public string Comment { get; set; } 

    //other properties 
} 

而在你View,使用方法如下

@Html.EditorFor(m => m.Comment) 
+0

它沒有工作。我已添加屏幕截圖以便更好地瞭解 – kandroid

0

其實你的代碼是非常混亂。在你的視圖中你沒有t use Model and seems you use m as your model and as i know this is completely wrong.i don t知道你的視圖正在渲染,但無論你使用m => m.sth,第二個m必須是nothin.indd你必須首先定義你的@model NoteCommentViewModel在cshtml文件中,然後使用Model而不是第二個m

+0

嗨..感謝您的寫作。但實際上,我添加了@ ViewModels.NoteCommentViewModel。我只是沒有寫在這裏.. :) – kandroid

相關問題