I created this class:
public class PageMeta
{
public string User { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Keywords { get; set; }
public string Url { get; set; }
public string UrlShort { get; set; }
public string InfoBox { get; set; }
}
And then this model:
pu开发者_开发知识库blic class HomeIndexViewModel
{
public HomeIndexViewModel()
{
PageMeta = new PageMeta();
}
public PageMeta PageMeta { get; set; }
}
And tried to use it like this:
var homeIndex = new HomeIndexViewModel { // 1
PageMeta.Title = "ABC" // 2
};
But it gives me an error in VS2010 saying:
- "Cannot initialize type HomeIndexViewModel with a collection initializer because it does not implement IEnumberable" "
- Cannot resolve symbol "Add" <- Message is in red and under where I mark //2
I hope someone can give me some advice. I did try commenting out the constructor public HomeIndexViewModel() but that didn't help me as I still get the same message.
Here's the correct syntax to use an object initializer in C#:
var homeIndex = new HomeIndexViewModel
{
PageMeta = new PageMeta
{
Title = "ABC",
User = "DEF"
}
};
精彩评论