I have a function which returns a JsonResult in the below way.
我有一个以下面的方式返回JsonResult的函数。
var attachments = (from a in ar.Attachments
select new { id = a.Id, filename = a.FileName }).ToArray();
var result = new
{
comments = "Some string",
attachments = attachments
};
return this.Json(result);
I need to use this result in another class where I need to access the "comments" and "attachments". Here attachments is a string array and comments is a string. Please let me know how I can do this.
我需要在另一个类中使用此结果,我需要访问“注释”和“附件”。这里的附件是一个字符串数组,注释是一个字符串。请让我知道如何做到这一点。
2 个解决方案
#1
1
You could create a ViewModel for the result and then just reuse that class. All a ViewModel is, is just a POCO or DTO. The idea is that it gives you a different way to "look" at your data, nothing special really.
您可以为结果创建ViewModel,然后只重用该类。所有的ViewModel都只是一个POCO或DTO。我们的想法是,它为您提供了一种“查看”数据的不同方式,没有什么特别的。
So you end up with 3 parts.
所以你最终得到3个部分。
The get data method:
获取数据方法:
public CommentsViewModel GetViewModel()
{
var attachments =
(from a in ar.Attachments
select new { id = a.Id, filename = a.FileName }).ToArray();
var result = new CommentsViewModel
{
comments = "Some string",
attachments = attachments
};
return result;
}
Your controller method:
你的控制器方法:
public JsonResult Get()
{
return this.Json(GetViewModel());
}
And your other method would just call GetViewModel() directly. This would separate this out a bit for you.
而你的另一种方法就是直接调用GetViewModel()。这会为你分开一点。
#2
0
Ok, so here is an answer that I believe should fit your needs using the dynamic
type...
好的,所以这里是一个我相信应该使用动态类型满足您需求的答案......
This is the method you call on the controller...I have put in 'hard coded' sample data as per your requirements for this example...I've removed the 's' from comments just because:
这是你在控制器上调用的方法......我根据你对这个例子的要求输入了“硬编码”的样本数据......我从注释中删除了's'只是因为:
public JsonResult GetJsonData()
{
var result = new
{
comment = "Some string",
attachments = new string[]{"/folder/file-1.jpg", "/folder/file-2.jpg"}
};
return this.Json(result);
}
The code that calls the controller action directly and reads the JsonResult:
直接调用控制器操作并读取JsonResult的代码:
dynamic result = GetJsonData().Data;
//var comment will result in a string which equals "Some string" in this example
var comment = result.comment;
//var attachments will result in a string[] which is equal to new string[]{"/folder/file-1.jpg", "/folder/file-2.jpg"}
var attachments = result.attachments;
#1
1
You could create a ViewModel for the result and then just reuse that class. All a ViewModel is, is just a POCO or DTO. The idea is that it gives you a different way to "look" at your data, nothing special really.
您可以为结果创建ViewModel,然后只重用该类。所有的ViewModel都只是一个POCO或DTO。我们的想法是,它为您提供了一种“查看”数据的不同方式,没有什么特别的。
So you end up with 3 parts.
所以你最终得到3个部分。
The get data method:
获取数据方法:
public CommentsViewModel GetViewModel()
{
var attachments =
(from a in ar.Attachments
select new { id = a.Id, filename = a.FileName }).ToArray();
var result = new CommentsViewModel
{
comments = "Some string",
attachments = attachments
};
return result;
}
Your controller method:
你的控制器方法:
public JsonResult Get()
{
return this.Json(GetViewModel());
}
And your other method would just call GetViewModel() directly. This would separate this out a bit for you.
而你的另一种方法就是直接调用GetViewModel()。这会为你分开一点。
#2
0
Ok, so here is an answer that I believe should fit your needs using the dynamic
type...
好的,所以这里是一个我相信应该使用动态类型满足您需求的答案......
This is the method you call on the controller...I have put in 'hard coded' sample data as per your requirements for this example...I've removed the 's' from comments just because:
这是你在控制器上调用的方法......我根据你对这个例子的要求输入了“硬编码”的样本数据......我从注释中删除了's'只是因为:
public JsonResult GetJsonData()
{
var result = new
{
comment = "Some string",
attachments = new string[]{"/folder/file-1.jpg", "/folder/file-2.jpg"}
};
return this.Json(result);
}
The code that calls the controller action directly and reads the JsonResult:
直接调用控制器操作并读取JsonResult的代码:
dynamic result = GetJsonData().Data;
//var comment will result in a string which equals "Some string" in this example
var comment = result.comment;
//var attachments will result in a string[] which is equal to new string[]{"/folder/file-1.jpg", "/folder/file-2.jpg"}
var attachments = result.attachments;