2014-01-05 40 views
0

我正在使用ASP.NET MVC 4并使用VS2013 pro。我有一个叫做Auction的模型如下:无法从视图中的对象访问属性

namespace auctionsite1.Models 
{ 
    public class Auction 
    { 
     public long id { set; get; } 
     public String title { set; get; } 
     public String description { set; get; } 
     public String imageurl { set; get; } 
     public DateTime starttime { get; set; } 
     public DateTime endtime { get; set; } 
     public decimal startprice { get; set; } 
     public decimal currentprice { get; set; } 
    } 
} 

有一个控制器拍卖和一个按名称索引的视图和视图。 现在我试图创建一个对象这个类的视图,并试图访问属性,但Visual Studio的2013是无法找到的对象的上下文名称:

@{ 
    var a = new auctionsite1.Models.Auction(); 
    { 
     **title** = "hi"; //unable to initialize title as it is not in context. 
     **id** = 123; 
     **starttime** = DateTime.Now; 
    }; 
} 

我无法理解为什么尽管明确地提到了班级及其路径,但仍是这样。

+3

在构造函数调用后删除分号。 – Shad

回答

1

正如@Shad所提到的,在构造函数后面有一个分号。如果要使用对象初始值设定项,则需要在设置属性后将其移至:

@{ 
    var a = new auctionsite1.Models.Auction() 
    { 
     title = "hi"; 
     id = 123; 
     starttime = DateTime.Now; 
    }; 
}