如何返回JsonResult [英] How to return JsonResult
问题描述
我在编写此AJAX方法时遇到麻烦.
I'm having trouble writing this AJAX method.
我想返回一个JsonResult
. (似乎是合乎逻辑的事情.)我可以找到的所有示例都使用Json()
将结果转换为JsonResult
.
I want to return a JsonResult
. (Seems like the logical thing to do.) All the examples I could find use Json()
to convert the result to a JsonResult
.
[HttpPost]
public JsonResult GetScoreResults(string userId, int chapterId)
{
return new Json(ChapterScoreResultsModel.Create(DbContext, userId, chapterId));
}
但是当我尝试这样做时:
But when I try this:
找不到类型或名称空间名称"Json"(您是否缺少using指令或程序集引用?)
The type or namespace name 'Json' could not be found (are you missing a using directive or an assembly reference?)
但是,我在任何地方都找不到该符号,而且我发现所有使用该符号的文章似乎都没有什么特别之处.我确实在命名空间System.Web.Helpers
中找到了该符号,但是当我为此添加using
时,出现了错误:
However, I can't find this symbol anywhere and all the articles I found that used it didn't seem to do anything special. I did find the symbol in the namespace System.Web.Helpers
but when I add a using
for that, I get the error:
无法创建静态类'Json'的实例
Cannot create an instance of the static class 'Json'
从AJAX方法返回JsonResult
的当前方法是什么?
What is the current way to return a JsonResult
from an AJAX method?
推荐答案
The Controller class has a Json method. Invoking it returns a JsonResult
. Note that it's method, not a constructor. You do not put the new
keyword in front of the method call.
return Json(ChapterScoreResultsModel.Create(DbContext, userId, chapterId));
这篇关于如何返回JsonResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!