ASP.NET MVC AJAX value not displaying
View:
function success(arg) {
var obj = arg.get_response().get_object();
if (obj.ErrorMessage === '') {
var answer = document.createElement('div');
answer.appendChild(document.createTextNode(obj.Answer));
document.getElementById('answers').appendChild(answer);
} else {
document.getElementById('errors').innerHTML = obj.ErrorMessage;
}
}
<% using (Ajax.BeginForm("EditOrganizationMeta", "Organization", new AjaxOptions { HttpMethod = "POST", OnSuccess = "success" })) { %>
<input type="submit" name="button<%=OrganizationMeta.vcr_MetaKey + Lang.int_LangId %>" value="Save" />
<div id="errors"></div>
<div id="answers"></div>
<% } %>
Controller:
[HttpPost]
[ValidateInput(false)]
public ActionResult EditOrganizationMeta(FormCollection collection)
{
return Json(new { Answer = "Record Successfully S开发者_如何学Caved", ErrorMessages = "Title is required" });
}
The thing is that success method in the javascript is not getting the required parameters. It is printing undefined there. Is there a problem in javascript method OnSuccess?
Firstly, quick debug suggestion:
function success(arg) {
alert(arg);
var obj = arg.get_response().get_object();
...
This is a simple check to see if you are getting anything passed back to you in the query.
Second suggestion:
What does this actually render as, when you view the source of the page...
<% using (Ajax.BeginForm("EditOrganizationMeta",
"Organization", new AjaxOptions {
HttpMethod = "POST", OnSuccess = "success" })) ...
You are looking for the bit where it calls the function titled "success" - does it pass the data into the function? If you're still stuck, update your question with the rendered HTML output for the form tag.
function success() {
var url = '<%= Url.Action("EditOrganizationMeta", "Organization") %>';
$.post(url, null, function(data) {
alert(data["Answer"]);
});
}
this works for me
精彩评论