views:

164

answers:

3

I've already started similar topic, but still didn't find final solution... So here I am with new one :) ... I'm developing NerdDinner from scratch and now I came to point where I define DinnerViewModel.
Following these instructions (starting from Listing 5) I came to this:

namespace Nerd.Controllers
{

    // View Model Classes  

    public class DinnerViewModel
    {
        public DinnerViewModel(List<Dinner> dinners)
        {
            this.Dinners = dinners;
        }

        public List<Dinner> Dinners { get; private set; }

    } 

    public class DinnerController : Controller
    {
        private DinnerRepository dinnerRepository = new DinnerRepository();

        ....

        public ActionResult NewDinners()
        {
            // Create list of products  
            var dinners = new List<Dinner>();
            dinners.Add(new Dinner(/*Something to add*/));

            // Return view  
            return View(new DinnerViewModel(dinners));
        }


    }
}

Also, the Dinner table in this new version of NerdDinner is a bit shortened (it contains of DinnerID, Title, EventDate and Description fields).

No matter what I try to add here dinners.Add(new Dinner(/*Something to add*/)); I always get following error

Error 1 'Nerd.Model.Dinner' does not contain a constructor that takes '1' arguments C:\Documents and Settings\ilija\My Documents\Visual Studio 2008\Projects\Nerd\Nerd\Controllers\DinnerController.cs 150 25 Nerd

Because I'm total beginner in C# and generally OOP, I have no idea what to do here... I suppose I need to declare a constructor, but how and where exactly?

Thanks,
Ile

+1  A: 

The exception message says it all. Your Dinner object doesn't have a constructor that takes 1 argument. So you cannot do this :

new Dinner(someVariable)

Because there is no method in the Dinner class that lets you create a dinner object with one argument.

çağdaş
so I need to create new (I suppose partial) class called Dinner.cs and create a constructor method?
ile
@ile, Either you have to create a constructor for Dinner class that accepts 1 argument, or you have to use an existing constructor of it while creating your ViewModel.
çağdaş
LukLed gave me the answer I was looking for. I don't know exactly what you meant with this existing constructor
ile
@ile, I think it would be a lot better if you first followed a tutorial on the C# language because otherwise you might find it difficult to understand ASP.NET MVC.
çağdaş
I have already, but in such short period I've read many tutorials so I have a big mess in my head. This requires a LOT of practice
ile
+1  A: 

If you've been following the nerd dinner "tutorial" you've probably used Linq2Sql and the default generated code define Dineer with parameterless constructor (method called 'Dinner()').

Instead you can use properties to set the object's values:

Dinner dinner = new Dinner;
dinner.Title = "My dinner";
dinner.Description ="...";
// etc.
Dror Helper
I've been following nerd dinner tutorial, all to the "Tests" section, but like I said, I'm totally new here and I couldn't catch it all from the first. There are some things differently done than I would like to...
ile
I treid this one before, but then I cannot pass the dinner object like this: return View(new DinnerViewModel(dinner));
ile
+2  A: 

If you want to initialize values in new Dinner object, use this construction

dinners.Add(new Dinner() { Title = "DinnerTitle", Description = "DinnerDescription" });
LukLed
This is it! I was trying all the time with normal brackets, never got it on my mind to try curly brackets. Thanks!
ile