Home Json Helper Class in Razor Pages Project
Reply: 1

Json Helper Class in Razor Pages Project

Michael
1#
Michael Published in 2018-02-09 16:32:39Z

I created a simple project and I can't seem to access the Json Helper class that I see through a bunch of sample projects. For example, I'm trying to use the Telerik dropdown sample and in the code below, I get the error:

The name Json does not exist in the current context.

(same thing goes for JsonRequestBehavior) What am I missing? I've done a search in nuget for System.Web.Helpers, but I can't add the reference because the project is targeted for ASP.NET Core 2.0 while the assembly was restored using .NET 4.6.1. What is my alternative?

public JsonResult RemoteDataSource_GetProducts(string text)
    {
        var northwind = new SampleEntities();
        var products = northwind.Products.Select(product => new ProductViewModel
        {
            ProductID = product.ProductID,
            ProductName = product.ProductName,
            UnitPrice = product.UnitPrice ?? 0,
            UnitsInStock = product.UnitsInStock ?? 0,
            UnitsOnOrder = product.UnitsOnOrder ?? 0,
            Discontinued = product.Discontinued
        });

        if (!string.IsNullOrEmpty(text))
        {
            products = products.Where(p => p.ProductName.Contains(text));
        }

        return Json(products, JsonRequestBehavior.AllowGet);
    }
Jaliya Udagedara
2#
Jaliya Udagedara Reply to 2018-02-09 16:55:39Z

Json(JsonResult) is a class and JsonRequestBehavior is an enum that lives within System.Web.Mvc in ASP.NET Framework. In ASP.NET Core, Json(JsonResult) lives within Microsoft.AspNetCore.Mvc.

You should be just fine with,

return Json(products);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.297866 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO