[英]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.JsonResult
或System.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.