像类型一样使用的变量

时间:2018-07-04 11:34:45

标签: c# asp.net-mvc

错误在返回语句中出现“'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);
    }

2 个答案:

答案 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);
    }