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 Dinner开发者_StackOverflowRepository 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,
IleIf you want to initialize values in new Dinner object, use this construction
dinners.Add(new Dinner() { Title = "DinnerTitle", Description = "DinnerDescription" });
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.
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.
精彩评论