Reputation: 2750
I have a very simple partial view and I am using ajaxform. In my partial view I have one sigle textArea and a Submit button. The problem is whatever I write into the text area, It does not send the data to controller, rather it sends a text which is = "Comment". If I dont write anything, the validation works perfect.
The Viewmodel:
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; }
}
Controller:
//
//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);
}
The view:
<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>
}
Here is the screen shot of the error...for better understanding. I am writing "data" in the view but in the controller I am getting "Comment"..Where is it coming from??
WOuld be great if someone can help me to identify the problem...!!
Upvotes: 0
Views: 259
Reputation: 139748
The problem is that your submit button's name
attribute is the same as the Comment
textarea name
attribute.
To resolve this you need to change the submit button's name
to something else than "comment" or remove the name
attribute from your submit button, so change:
<input type="submit" value="Comment" name="comment" class="btn"/>
To
<input type="submit" value="Comment" class="btn"/>
Because the Ajax.BeginForm
uses the the jQuery .serializeArray() method which - because your submit button has a name
and this input triggers the submit - also sends the submit button's value "Comment"
to the server.
Upvotes: 1
Reputation: 35
actually your code is very confusing. in your view u didnt use Model and seems you use m as your model and as i know this is completely wrong.i don
t know your view is rendering but wherever you use m=>m.sth the second m must be nothin.instead you must define your @model NoteCommentViewModel at first of cshtml file and then use Model instead of second m
Upvotes: 0
Reputation: 218722
I am not sure what exactly your problem is. But the below code should work.
public class NoteCommentViewModel
{
public Int32 Id { get; set; }
[Required(ErrorMessage=" Your Error message")]
[DataType(DataType.MultilineText)]
public string Comment { get; set; }
//other properties
}
And in your View
, Use it like this
@Html.EditorFor(m => m.Comment)
Upvotes: 1