猿问

像类型一样使用的变量

错误来自 return 语句“'mydata' 是一个变量,但用作类型”如何修复错误?


    [HttpGet]

    public JsonResult NewData()

    {

        List<mydatasample> mydata = new List<mydatasample>(); 

        mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });

        mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });

        mydata.Add(new mydatasample { bookName = "test2", publisherName = "yum3", publishYear = 2018 });

        mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });

        mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });

        mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });

        mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });

        mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });

        mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });

        mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });




        return Json(new mydata { }, JsonRequestBehavior.AllowGet);

    }


梦里花落0921
浏览 165回答 2
2回答

慕姐4208626

您正在 return 语句上创建另一个对象。请从返回行中删除 new 关键字和 {}。[HttpGet]public JsonResult NewData(){&nbsp; &nbsp; List<mydatasample> mydata = new List<mydatasample>();&nbsp;&nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test2", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; return Json(mydata, JsonRequestBehavior.AllowGet);}

慕码人8056858

正如评论中所说的错误是在返回&nbsp; &nbsp;[HttpGet]&nbsp; &nbsp; public JsonResult NewData()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<mydatasample> mydata = new List<mydatasample>();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; &nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; &nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test2", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; &nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; &nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; &nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; &nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; &nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; &nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; &nbsp; &nbsp; mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });&nbsp; &nbsp; &nbsp; &nbsp; return Json(mydata, JsonRequestBehavior.AllowGet);&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答