无法在ASP.NET中实例化Object

时间:2014-09-26 18:21:10

标签: c# asp.net asp.net-mvc-4

我有一个名为' Repository'它用数据实例化一些样本类:

public class Repository
{
    // create dictionary collection for prices, and define property to get the collection
    Dictionary<string, int> prices = new Dictionary<string, int>();
    public Dictionary<string, int> Prices { get { return prices; } }

    // create List with Reservations, and define property to get the List 
    List<Reservation> reservations = new List<Reservation>();
    public List<Reservation> Reservations { get { return reservations; } }
    public Repository()
    {

        // add prices to the dictionary, prices
        prices.Add("Dog", 180);
        prices.Add("Cat", 140);
        prices.Add("Snake", 120);
        prices.Add("Guinea pig", 75);
        prices.Add("Canary", 60);

        // create customers
        Customer c1 = new Customer(1, "Susan", "Peterson", "Borgergade 45", "8000", "Aarhus", "supe@xmail.dk", "21212121");
        Customer c2 = new Customer(2, "Brian", "Smith", "Algade 108", "8000", "Aarhus", "brsm@xmail.dk", "45454545");
        Reservation r1 = new Reservation(1, "Hamlet", new DateTime(2014, 9, 2), "Dog", new DateTime(2014, 9, 20), new DateTime(2014, 9, 20), c1);
        Reservation r2 = new Reservation(2, "Dog", new DateTime(2014, 9, 14), "Samson", new DateTime(2014, 9, 21), new DateTime(2014, 9, 21), c1);
        Reservation r3 = new Reservation(3, "Cat", new DateTime(2014, 9, 7), "Darla", new DateTime(2014, 9, 10), new DateTime(2014, 9, 10), c2);

        // add Reservations to list of Reservations with instance name reservations
        reservations.Add(r1);
        reservations.Add(r2);
        reservations.Add(r3);
    }
}

现在我想在视图中显示该数据,因此我尝试在ReservationController中对其进行实例化,并将其提供给视图:

public ActionResult Index()
{
    private Repository repository = new Repository();
    return View(repository.Reservations);
}

这会在我尝试实例化存储库的行上产生一些错误:

  

类型或命名空间名称&#39;存储库&#39;找不到(是你吗?   缺少using指令或程序集引用?)

     

预期

     

无效的表达术语&#39; private&#39;

1 个答案:

答案 0 :(得分:3)

访问修饰符分配给局部变量无效,因此错误。

您需要从局部变量中删除private访问修饰符。

public ActionResult Index()
{
  Repository repository = new Repository();
  return View(repository.Reservations);
}