Action method return raw json data instead of view in ASP.NET Core 2.2

前端 未结 1 858
暖寄归人
暖寄归人 2021-01-29 01:30

I want to get data from the database and send it as JSON to view to fill datatable with it, but action method returns raw JSON data.

My action method

            


        
1条回答
  •  情话喂你
    2021-01-29 01:43

    Here is a working demo like below:

    1.View(Index.cshtml):

    id goodsName scale action
    id goodsName scale action
    @section Scripts{ }

    2.Controller:

    public IActionResult Index()
    {
        return View();
    }
    public IActionResult GoodsList()
    {
        var goodsScale =  new List
        {
            new {id = 1, goodsName= "aa",scale="a"},
            new {id = 2, goodsName= "bb",scale="b"},
            new {id = 3, goodsName= "cc",scale="c"},
            new {id = 4, goodsName= "dd",scale="d"} 
        };
        return Json(new { data=goodsScale });
    }
    
    
    

    3.Result(the url should be:/home/index):

    0 讨论(0)
    提交回复
    热议问题