JHarley1
JHarley1

Reputation: 2112

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 ClassifiedsListingDetailsViewModel
    {
        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

Upvotes: 0

Views: 224

Answers (1)

BeRecursive
BeRecursive

Reputation: 6376

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"
        };

Upvotes: 2

Related Questions