Reputation: 370
I want to call partial view after calling ActionResult with ajax hit and render partial view data into main view. But when calling to ActionResult, its calling perfectly but not calling to Partial view and returning error message in ajax. Ajax method is as:
function postComment() {
var comment = $("#addComment").val();
var pathname = window.location.pathname.split("/");
var title = pathname[pathname.length - 1];
alert(comment);
jQuery.ajax({
type: 'POST',
dataType: "html",
url: "/Blogger/PostComment",
data: JSON.stringify({ 'comment': comment, "title": title ,"type":"Blogs"}),
contentType: "application/json; charset=utf-8",
success: function (result) {
$("#divAnswerSchemeContainer1").html(result);
},
error: function (e) {
alert("error");
}
});
}
Action Result is:
public ActionResult PostComment(string comment, string title, string type) {
return PartialView("~/Views/Shared/_partialShowComments", db.SocialMediaComments.ToList());
}
And the partial view is:
@model IEnumerable<DataModel.SocialMediaComment>
<div id="divAnswerSchemeContainer1">
@foreach (var item in Model)
{
@item.Comment
@item.CreatedDate
@item.BlogeName
}
</div>
Action result calling fine with ajax but partial view not hitting well and returning error message from ajax.
Browser console showing error message:
"NetworkError: 500 Internal Server Error - http://localhost:6767/Blogger/PostComment"
Upvotes: 0
Views: 526
Reputation: 4494
At first you have to create viewmodel.
public class Post{
public string comment {get;set;}
public string title {get;set;}
public string type {get;set;}
}
you action controller be like.
public ActionResult PostComment(Post post) {
return PartialView("~/Views/Shared/_partialShowComments", db.SocialMediaComments.ToList());
}
Upvotes: 1