我将Json发送到mvc方法并根据模式检查它是否正确的键/值对。如果它通过,我想将用户发送到同一控制器上的另一个视图,但如果Json失败,我想在模态对话框中显示缺少的键/值对。由于某种原因,当我返回Json时,它并没有与视图相对应,我无法弄清楚原因。这是我的代码:
型号:
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);
}
}
我的观点:
$('#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");
}
});
});
console.log(JsonResults);
显示[对象,对象]。我做错了什么?
答案 0 :(得分:0)
谢谢Stephen,这就是答案:(JsonResults[0].Success === true) {
一旦实施,它就有效了。