错误在返回语句中出现“'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);
}
答案 0 :(得分:2)
您正在return语句上创建另一个对象。请从返回行中删除新关键字和{}。
[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(mydata, JsonRequestBehavior.AllowGet);
}
答案 1 :(得分:0)
如评论中所述,错误位于Return
[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(mydata, JsonRequestBehavior.AllowGet);
}