开发者

ASP.NET MVC2 View Models Basic Question

Good Morning,

I have a very simple question regarding ASP.NET MVC 2 View Models.

I have created the following view model:

 public class Class开发者_开发百科ifiedsListingDetailsViewModel
    {
        public int ListingID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string ImageURL { get; set; }
        public string Location { get; set; }
        public string ListedBy { get; set; }
        public string ContactDetails { get; set; }
        public string CategoryName { get; set; }
    }

No problems, the problems occur when trying to set the values in the controller:

var listing = db.Listings.Single(l => l.ListingID == id);

        var viewModel = new ClassifiedsListingDetailsViewModel
        {
            ListingID = listing.ListingID;


        };

When ever I trying and set ListingID, which is the first property of the view model it wants me to add a "," rather than a ";". Not sure how to overcome this?

Many Thanks, J


This has to do with instantiating new objects. If you want to do as you say then simply write:

var listing = db.Listings.Single(l => l.ListingID == id);

        var viewModel = new ClassifiedsListingDetailsViewModel
        {
            ListingID = listing.ListingID    
        };

If you want to instantiate more fields then use the comma as you say:

        var viewModel = new ClassifiedsListingDetailsViewModel
        {
            ListingID = listing.ListingID,
            Title = "Title String"
        };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜