从mvc控制器将Json返回到ajax成功
我将Json发送到mvc方法,并根据模式检查它是否有正确的键/值对。如果通过,我希望将用户发送到同一控制器上的其他视图,但如果Json失败,我希望在模式对话框中显示缺少的键/值对。由于某种原因,当我返回Json时,它没有出现在视图中,我不知道为什么。这是我的密码: 型号:从mvc控制器将Json返回到ajax成功,json,ajax,asp.net-mvc,Json,Ajax,Asp.net Mvc,我将Json发送到mvc方法,并根据模式检查它是否有正确的键/值对。如果通过,我希望将用户发送到同一控制器上的其他视图,但如果Json失败,我希望在模式对话框中显示缺少的键/值对。由于某种原因,当我返回Json时,它没有出现在视图中,我不知道为什么。这是我的密码: 型号: public class EditReportResponseJon { public string ErrorPath { get; set; } public string ErrorKind {
public class EditReportResponseJon
{
public string ErrorPath { get; set; }
public string ErrorKind { get; set; }
public Boolean Success { get; set; }
}
控制器:
[HttpPost]
public ActionResult SubmitReport(string JsonStringSend)
{
dynamic JSend = JObject.Parse(JsonStringSend);
var schema = JsonSchema4.FromType<ReportItem>();
var schemaData = schema.ToJson();
var errors = schema.Validate(JSend.JsonString);
schema = JsonSchema4.FromJson(schemaData);
if (errors.Count > 0)
{
foreach (var error in errors)
Console.WriteLine(error.Path + ": " + error.Kind);
List<EditReportResponseJon> JsonResults = new List<EditReportResponseJon>{
new EditReportResponseJon{Success = false, ErrorKind = "The error", ErrorPath = "The path of the error"}
};
return Json(JsonResults, JsonRequestBehavior.AllowGet);
}
else
{
List<EditReportResponseJon> JsonResults = new List<EditReportResponseJon>{
new EditReportResponseJon{Success = false, ErrorKind = "", ErrorPath = ""}
};
return Json(JsonResults, JsonRequestBehavior.AllowGet);
}
}
console.log(JsonResults)代码>显示[对象,对象]。我做错了什么?谢谢你,Stephen,这是答案:(JsonResults[0]。成功===true){
一旦实现它就成功了。将返回类型改为JsonResult而不是ActionResult。我仍然得到了[object object]
当我使用JsonResult
时返回。这真的很奇怪。你期望什么。你发送了一个集合(不确定为什么它只包含一个对象),所以你需要if(JsonResults[0]。Success==true){
$('#SubmitReport').on("click", function () {
editor.session.getUndoManager().markClean();
//buttonpressed = true;
//window.onbeforeunload=null;
//Hide the error messages if they are showing.
$('#ReportNameError').css('visibility', 'hidden');
$('#DocumentMsg').css('visibility', 'hidden');
var EditorsValue = jQuery.parseJSON(editor.getValue());
var UserReportFilename = $('#JsonFilename').val();
var JsonStringSend = JSON.stringify({ JsonString: EditorsValue, UserReportFilename: UserReportFilename });
//Send the Json to the server
$.ajax({
url: "@Url.Action("SubmitReport", "Reporting", null)",
type: "POST",
dataType: "json",
data: { 'JsonStringSend': JsonStringSend },
success: function (JsonResults, status) {
console.log(JsonResults);
if (JsonResults.Success === true){
//Redirect user to the Report Queue if success
@*var link = '@Url.Action("ReportManagement", "Reporting", null)';
window.location.href = link;*@
console.log('success!')
}
if (JsonResults.Success === false) {
console.log('Unsuccessful!')
//show error div with errors.
}
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus + ": " + errorThrown + "." + " Please see above if applicable");
}
});
});