Json Does Not Exist in Current Context RRS feed

  • Question

  • User-939035612 posted

    I am porting an example for a MVC Core tutorial over to .Net Core and it seem great except when it is finished I get a red squiggly line under the "Json" saying it does not exist in the current context. The tutorial is at https://www.c-sharpcorner.com/article/creating-simple-cascading-dropdownlist-in-asp-net-core-mvc-with-new-tag-helpers/ and the code rendering the error is as follows:

    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using PostAlmostAnything.Data;
    using PostAlmostAnything.Models;
    using PostAlmostAnything.SiteServices;
    using System;
    using System.Collections;
    using System.ComponentModel.DataAnnotations;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Net.Http;
    using MimeKit;
    using Newtonsoft.Json.Linq;
    using MailKit.Net.Smtp;
    using System.Collections.Generic;
    using System.Linq;
    //Irrelevant Code Blocks Removed//
    public IActionResult OnGet()
    			List<Categories> categorylist = new List<Categories>();
    			// ------- Getting Data from Database Using EntityFrameworkCore -------
    			categorylist = (from cats in _context.Categories select cats).ToList();
    			// ------- Inserting Select Item in List -------
    			categorylist.Insert(0, new Categories { Categoryid = 0, Categoryname = "Select" });
    			// ------- Assigning categorylist to ViewBag.ListofCategory -------
    			ViewData["ListofCategories"] = categorylist;
    			return Page();
    public JsonResult GetSubCategory(int Categoryid)
    			List<Subcategories> Subcategorylist = new List<Subcategories>();
    			// ------- Getting Data from Database Using EntityFrameworkCore -------
    			Subcategorylist = (from scat in _context.Subcategories
    							   where scat.Category == Categoryid
    							   select scat).ToList();
    			// ------- Inserting Select Item in List -------
    			Subcategorylist.Insert(0, new Subcategories { Subcategoryid = 0, Subcategoryname = "Select" });
    			return Json(new SelectList(Subcategorylist, "Subcategoryid", "Subcategoryname"));

    The red squiggly line appears under "Json" on the line that reads return Json(new SelectList(Subcategorylist, "Subcategoryid", "Subcategoryname"));

    Tuesday, October 6, 2020 11:36 PM


  • User-939035612 posted

    I got it working using "new JsonResult" instead

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 13, 2020 11:20 PM

All replies

  • Wednesday, October 7, 2020 12:16 AM
  • User-939035612 posted

    It isn't really helpful at all. The demo does not use any of those packages.

    Wednesday, October 7, 2020 12:57 AM
  • User-474980206 posted

    my guess is the the 

    //Irrelevant Code Blocks Removed//

    does not make these controller actions.

    Wednesday, October 7, 2020 1:06 AM
  • User-939035612 posted

    I've tried creating an api controller called CategoryLists.cs and moving GetSubCategory to it and I still get the same result. I am using Razor Pages so I need to modify the tutorial to fit those. Usually that involves calling the DB context both from the page and from the controller. It requires the page to get the list of categories and then the api controller has to get sub categories based on category ID. 

    I'm a little confused as to what should be put in an Index.cshtml.cs file and what should go in a controller. I am also confused as to how the demo knows which controllers go with which views because I do not see any references to the controllers in the views in the demo.

    Wednesday, October 7, 2020 1:07 AM
  • User-939035612 posted

    I managed to get rid of the red squiggly lines by changing "Json" to "new JsonResult" Hopefully that works

    Wednesday, October 7, 2020 1:22 AM
  • User753101303 posted

    Yep, Json  is a shortcut helper which is available in Controllers. 

    Unless experienced already with the stuff you are studying it's likely best to follow a tutorial matching closely what you want to do rather than adapting a tutorial. So if using Razor pages see rather https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-3.1 or https://www.learnrazorpages.com/razor-pages/

    You can use both MVC or Razor Pages in  the same project but for a given "web page" you'll use either MVC or Razor Pages (which is a kind of "simplified MVC" if you want).

    Edit: I believe that what could be confusing is that the Razor syntax being used in both cases you have Razor Views on the MVC side which could be easily confused with Razor Pages.

    Thursday, October 8, 2020 2:47 PM
  • User-939035612 posted

    I got it working using "new JsonResult" instead

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 13, 2020 11:20 PM