C# 在Asp.net核心中使用Json发送模型
我想将模型从controller传递到视图中的Success方法,并使用json访问模型中的属性。如何在Success方法中写入和访问属性C# 在Asp.net核心中使用Json发送模型,c#,jquery,asp.net-mvc,C#,Jquery,Asp.net Mvc,我想将模型从controller传递到视图中的Success方法,并使用json访问模型中的属性。如何在Success方法中写入和访问属性 公共异步任务编辑(部门) { if(ModelState.IsValid) { _一般报告。更新(部门); wait _genericpository.SaveChangesAsync(); var模型=\u genericRepository.GetByIdAsync(department.department\u Id); 返回Json(新的{Messa
公共异步任务编辑(部门)
{
if(ModelState.IsValid)
{
_一般报告。更新(部门);
wait _genericpository.SaveChangesAsync();
var模型=\u genericRepository.GetByIdAsync(department.department\u Id);
返回Json(新的{Message=model});
}
返回(部门);
}
功能成功(数据)
{
警报(data.Messge);
}
函数失败(){
}
使用json.Parse()时,您可以从json文件访问数据列表
代码>
功能成功(数据)
{
var getDataList=Json.parse(数据);
警报(getDataList);
}
函数失败(){
}
那么:
$.ajax({
url: "<path>/Edit",
type: "POST",
data: JSON.stringify(department),
dataType: "json",
cache: false,
success: function (data) {
[...]
},
error: function () {
[...]
}
})
$.ajax({
url:“/Edit”,
类型:“POST”,
数据:JSON.stringify(部门),
数据类型:“json”,
cache:false,
成功:功能(数据){
[...]
},
错误:函数(){
[...]
}
})
然后使用JSON解析器解析响应。现在您可以像这样从ajax成功中读取属性值
success: function (response) {
if (response.length == 0)
alert('Some error occured while uploading');
else {
var obj = JSON.parse(response);
$('#divPrint').html(obj.bodyone);
$('#divPrint2').html(obj.bodytwo);
$('#fileName1').html(obj.FileName1);
$('#fileName2').html(obj.FileName2);
$('#headersFile1').html(obj.HeadersFile1);
$('#headersFile2').html(obj.HeadersFile2);
}
}
使用AJAX调用方法并成功返回数据。!我的表单使用ajax,如何使用json将我的模型从控制器发送到视图@Mohansrinivas指的是这个和这个
$.ajax({
url: "<path>/Edit",
type: "POST",
data: JSON.stringify(department),
dataType: "json",
cache: false,
success: function (data) {
[...]
},
error: function () {
[...]
}
})
public ActionResult Import()
{
string response = JsonConvert.SerializeObject(responseModel);
return this.Content(response);
}
class ResponseModel
{
public string bodyone { get; set; }
public string bodytwo { get; set; }
public string HeadersFile1 { get; set; }
public string HeadersFile2 { get; set; }
public string FileName1 { get; set; }
public string FileName2 { get; set; }
}
success: function (response) {
if (response.length == 0)
alert('Some error occured while uploading');
else {
var obj = JSON.parse(response);
$('#divPrint').html(obj.bodyone);
$('#divPrint2').html(obj.bodytwo);
$('#fileName1').html(obj.FileName1);
$('#fileName2').html(obj.FileName2);
$('#headersFile1').html(obj.HeadersFile1);
$('#headersFile2').html(obj.HeadersFile2);
}
}