由于模型绑定接收数据经常出现问题,所以使用了原始的方法接收从前台用ajax提交过来的数据:
下面是示例代码:
前台:
// 表单提交
form.on('submit(formSubmit)', function (data) {
layer.msg(JSON.stringify(data.field));
//data.field.F_Photo = document.getElementById("preview").src;
delete data.field.file;
// ajaxa提交
console.log(JSON.stringify(data.field));
var formData = JSON.stringify(data.field);
console.log('formData: ' + formData);
$.ajax({
url: "/TrainingManage/Certificate/Get",
data: formData,
processData: false,
contentType: false,
type: "post",
dataType: "json",
success: function (data) {
if (data.state == "success") {
$('#picCertificate').attr("src", data.data);
console.log(data.data);
layer.alert(data.message, {
icon: 1,
skin: 'layer-ext-moon'
});
} else {
layer.alert(data.message, {
icon: 1,
skin: 'layer-ext-moon'
});
}
},
error: function (data) {
layer.alert('提交数据失败', {
icon: 1,
skin: 'layer-ext-moon'
});
}
});
});
后台接收数据:
public ActionResult GetData()
{
var sr = new StreamReader(Request.InputStream);
var stream = sr.ReadToEnd();
var entity = JsonConvert.DeserializeObject<CertificateEntity>(stream);
if (ModelState.IsValid)
{
...
return Content(resultdata.ToJson());
}
var result = new
{
state = "fail",
message = "生成证书失败",
data = ""
};
return Content(result.ToJson());
}