2017-08-16 95 views
-1

我在寫這個AJAX方法時遇到了麻煩。我想返回JsonResult。 (看起來像是合乎邏輯的事情。)我能找到的所有示例都使用Json()將結果轉換爲JsonResult如何返回JsonResult

[HttpPost] 
public JsonResult GetScoreResults(string userId, int chapterId) 
{ 
    return new Json(ChapterScoreResultsModel.Create(DbContext, userId, chapterId)); 
} 

但當我嘗試這個辦法:

The type or namespace name 'Json' could not be found (are you missing a using directive or an assembly reference?)

但是,我找不到這個符號的任何地方,我發現用它似乎並沒有做什麼特別的所有文章。我發現在命名空間System.Web.Helpers符號,但是當我添加using的是,我得到的錯誤:

Cannot create an instance of the static class 'Json'

什麼是當前的方式來從AJAX方法返回一個JsonResult

+1

刪除'new' - 只是'返回JSON(...)' –

+1

new關鍵字不需要 http://www.c-sharpcorner.com/UploadFile/2ed7ae/jsonresult-type-in​​- mvc/ – Amit

+0

@StephenMuecke:OMG!我不敢相信我沒有看到。 (我添加了新的,因爲最初我沒有使用'Json'。 –

回答

0

控制器類有Json method。調用它將返回JsonResult。請注意,它是方法,而不是構造函數。您不要將new關鍵字放在方法調用之前。

return Json(ChapterScoreResultsModel.Create(DbContext, userId, chapterId)); 
相關問題