के साथ फ़ॉर्म सबमिट करें मैं एमवीसी सीखने की कोशिश कर रहा हूं और एक चीज जो मैं करना चाहता हूं वह मेरे नियंत्रक में एक क्रिया सबमिट करने के लिए है और यह क्रिया सबमिट किए गए डेटा को वापस कर देगी। सरल लगता है लेकिन मैं बिना किसी सफलता के घंटों तक कोशिश कर रहा हूं। मेरे विचार:jquery AJAX
@using (Html.BeginForm("BlogComment","Blog"))
{
@Html.ValidationSummary(true)
<legend class="AddAComment">Add a comment</legend>
<div class="commentformwrapper">
<div class="editor-text">
<span class="editor-label">User Name:</span>
</div>
<div class="editor-text">
<input type="text" id="username" />
</div>
<div class="editor-text">
<textarea id="comment" rows="6" cols="23"></textarea>
</div>
<div class="editor-field">
<input type="hidden" id="hiddendate" />
</div>
<input type="submit" id="submit" value="Create" />
</div>
}
मेरी नियंत्रक:
[HttpPost]
public ActionResult CommentForm(Comment comment)
{
Comment ajaxComment = new Comment();
ajaxComment.CommentText = comment.UserName;
ajaxComment.DateCreated = comment.DateCreated;
ajaxComment.PostId = comment.PostId;
ajaxComment.UserName = comment.UserName;
mRep.Add(ajaxComment);
uow.Save();
//Get all the comments for the given post id
return Json(ajaxComment);
}
और मेरे js:
$(document).ready(function() {
$('form').submit(function() {
$.ajax({
url: '@Url.Action("CommentForm")',
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: {
PostId: $('.postid').val(),
UserName: $('#username').val(),
DateCreated: new Date(),
CommentText: $('#comment').val()
},
success: function (result) {
alert("success " + result.UserName);
},
error: function (result) {
alert("Failed");
}
});
return false;
});
});
आपकी समस्या क्या है? क्या काम नहीं कर रहा है – nemesv
आप JSON.stringify() –