简体   繁体   English

ASP.NET MVC Viewmodel麻烦

[英]ASP.NET MVC Viewmodel trouble

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. 我已经开始了类似的话题 ,但仍然没有找到最终的解决方案...所以我在这里与新的一个:) ...我正在从头开发NerdDinner,现在我来点我定义DinnerViewModel。
Following these instructions (starting from Listing 5 ) I came to this: 按照这些说明 (从清单5开始),我来到了这个:

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). 此外,这个新版本的NerdDinner中的Dinner表有点缩短(它包含DinnerIDTitleEventDateDescription字段)。

No matter what I try to add here dinners.Add(new Dinner(/*Something to add*/)); 无论我在这里添加什么dinners.Add(new Dinner(/*Something to add*/));添加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 错误1'Nerd.Model.Dinner'不包含带'1'参数的构造函数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? 因为我是C#的初学者,一般都是OOP,我不知道该怎么做......我想我需要声明一个构造函数,但是究竟是怎么做的?

Thanks, 谢谢,
Ile

如果要初始化新Dinner对象中的值,请使用此构造

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. 您的Dinner对象没有带1个参数的构造函数。 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. 因为Dinner类中没有允许您使用一个参数创建一个晚餐对象的方法。

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()'). 如果您一直在关注书呆子晚餐“教程”,您可能已经使用了Linq2Sql,默认生成的代码使用无参数构造函数定义Dineer(方法称为'Dinner()')。

Instead you can use properties to set the object's values: 相反,您可以使用属性来设置对象的值:

Dinner dinner = new Dinner;
dinner.Title = "My dinner";
dinner.Description ="...";
// etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM