2014-09-26 37 views
-6

我有一个名为“资源库”类实例与数据一些样本的类实例化对象:无法在ASP.NET

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", "[email protected]", "21212121"); 
     Customer c2 = new Customer(2, "Brian", "Smith", "Algade 108", "8000", "Aarhus", "[email protected]", "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,并将其提供给View:

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

这将产生一对夫妇就行了错误的,我尝试实例存储库:

无法找到类型或命名空间名称'Repository'(您是否缺少使用指令或程序集引用的 ?)

;预期

无效表达术语“私人”

+0

感谢您使用_actual_构建错误来解决问题。或者它发生的地方。 – HABO 2014-09-26 18:25:09

+1

我只在您的存储库类型上看到价格属性。然而,你引用一个保留属性? – BlakeH 2014-09-26 18:26:24

+0

@SonicTheLichen,当我编辑我的编辑(主要是让VS做格式化)时,我在Repository类中添加了一个结尾括号。虽然这意味着课堂上还有更多未发布的内容,但并未改变_it未被发布的事实。 – gunr2171 2014-09-26 18:31:50

回答

3

是无效的一个访问修饰符分配给本地变量因此错误。

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

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

我注意到您收回了您的接受投票。我只是想知道为什么?我知道答案并不是特别令人印象深刻,但它确实回答了这个问题。 – 2014-09-28 09:18:48