简体   繁体   English

如何解决Razor Pages错误“ CS0103当前上下文中不存在名称'Json'”?

[英]How do I solve the Razor Pages error “CS0103 The name 'Json' does not exist in the current context”?

I am using Razor Pages (not MVC) and keep getting the above error on the return statement. 我正在使用Razor Pages(不是MVC),并在return语句上不断出现上述错误。 There is a similar question out there relating to the MVC model but the answer to that one is to change the class to "Controller". 关于MVC模型,存在类似的问题,但是对此的答案是将类更改为“ Controller”。 When I try that, page related things break. 当我尝试这种操作时,与页面相关的东西就会中断。 Any suggestions? 有什么建议么?

public class VehicleInfoPageModel : PageModel
{        
    public SelectList ModelNameSL { get; set; }

public JsonResult PopulateModelDropDownList(StockBook.Models.StockBookContext _context,
        int selectedMakeID,
        object selectedModelID = null)
    {
        var ModelIDsQuery = from m in _context.VehicleModel
                            orderby m.ModelID // Sort by ID.
                            where m.MakeID == selectedMakeID
                            select m;

        ModelNameSL = new SelectList(ModelIDsQuery.AsNoTracking(),
                    "ModelID", "ModelName", selectedModelID);
        return Json(ModelNameSL);
    }

You're tried to use Json() method which derived from either System.Web.Mvc.JsonResult or System.Web.Http.ApiController.JsonResult instead of Microsoft.AspNetCore.Mvc.JsonResult namespace, they're all different namespaces. 您尝试使用从System.Web.Mvc.JsonResultSystem.Web.Http.ApiController.JsonResult派生的Json()方法,而不是Microsoft.AspNetCore.Mvc.JsonResult命名空间,它们都是不同的命名空间。 You should use the constructor of Microsoft.AspNetCore.Mvc.JsonResult to create JSON string instead: 您应该使用Microsoft.AspNetCore.Mvc.JsonResult的构造函数来创建JSON字符串:

public JsonResult PopulateModelDropDownList(StockBook.Models.StockBookContext _context, int selectedMakeID, object selectedModelID = null)
{
    var ModelIDsQuery = from m in _context.VehicleModel
                        orderby m.ModelID // Sort by ID.
                        where m.MakeID == selectedMakeID
                        select m;

    ModelNameSL = new SelectList(ModelIDsQuery.AsNoTracking(),
                "ModelID", "ModelName", selectedModelID);

    // return JSON string
    return new JsonResult(ModelNameSL);
}

Reference: Working With JSON in Razor Pages 参考: 在Razor页面中使用JSON

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决错误CS0103:当前上下文中不存在名称'minutes' - How to solve error CS0103: the name 'minutes' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“_context” - Error CS0103: The name '_context' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“currentScreen”(CS0103) - Error CS0103: The name 'currentScreen' does not exist in the current context (CS0103) 错误CS0103:名称“ TimeSpan”在当前上下文(CS0103)(testingProgram)中不存在? - Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)? 错误cs0103名称'IEnumerator'在当前上下文中不存在 - error cs0103 The Name 'IEnumerator' does not exist in the current context 错误CS0103:名称“ HttpUtility”在当前上下文中不存在 - error CS0103: The name `HttpUtility' does not exist in the current context "错误 CS0103:当前上下文中不存在名称“AssetPreview”" - error CS0103: The name 'AssetPreview' does not exist in the current context 错误CS0103当前上下文中不存在名称“图像” - Error CS0103 The name 'Image' does not exist in the current context Unity错误CS0103:当前上下文中不存在名称`' - Unity error CS0103: The name `' does not exist in the current context 错误 CS0103: 当前上下文中不存在名称“ ” - error CS0103: The name ' ' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM