从另一个模型的菜创建一个菜单,一个DropDownList一个创建视图 [英] Create a Create view for a Menu With a Dropdownlist from another Model for the dishes

查看:229
本文介绍了从另一个模型的菜创建一个菜单,一个DropDownList一个创建视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个学生,需要做的学校申请。
这是一个餐厅预订申请。
我想创建的菜单,在这里你可以给菜单中的名称创建视图,您选择哪道菜本菜单都需要有。这些菜存储在另一个模型。
我使用了这个问题:<一href=\"http://stackoverflow.com/questions/5326515/create-a-dropdown-list-for-mvc3-using-entity-framework-edmx-model-razor-vie\">Create一个下拉列表,使用实体框架(型号的.edmx)及MVC3;剃刀观点与放大器;&安培;插入数据库记录到多个表但不能让它开始工作。
这是我的模型:

i'm a student and needs to make a application for school. This is a reservation application for a restaurant. I want to create a Create view for the menu where you can give the menu a name and you choose which dishes this menu needs to have. These dishes are stored in another model. I'm using this question: Create a Dropdown List for MVC3 using Entity Framework (.edmx Model) & Razor Views && Insert A Database Record to Multiple Tables but not get it to work. This is my model:

public class MenuAddController
{
    private BonTempsDbContext db = new BonTempsDbContext();
    public Menu Menu { get; set; }
    public Dictionary<int, string> Voorgerechten { get; set; }
    public Dictionary<int, string> Hoofdgerechten { get; set; }
    public Dictionary<int, string> Nagerechten { get; set; }

    public MenuAddController() { }
    public MenuAddController(int id)
    {
        Menu = db.Menu
            .Where(e => e.id == id).SingleOrDefault();

        // instantiate your dictionaries

        foreach (var Voorgerecht in db.Voorgerecht)
        {
            Voorgerechten.Add(Voorgerecht.id, Voorgerecht.description);
        }
        foreach (var Hoofdgerecht in db.Hoofdgerecht)
        {
            Hoofdgerechten.Add(Hoofdgerecht.id, Hoofdgerecht.description);
        }
        foreach (var Nagerecht in db.Nagerecht)
        {
            Nagerechten.Add(Nagerecht.id, Nagerecht.description);
        }

    }
}

这是我的控制器。

And this is my Controller.

public class MenusController : Controller
{
    // GET: Menus
    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public ActionResult Add()
    {
        return View(new MenuAddController());
    }

    [HttpPost]
    public ActionResult Add(MenuAddController vm)
    {
        if (ModelState.IsValid)
        {
            Menu.Add(vm.Menu); <--- this is the error line
            return View("Index"); // or wherever you go after successful add
        }

        return View(vm);
    }
}

这给出了一个错误

It gives an error on

Menu.Add(vm.Menu);

错误1'BonTempsMVC.Menu'不包含'添加'的定义

Error 1 'BonTempsMVC.Menu' does not contain a definition for 'Add'

任何人都可以帮助我?

推荐答案

您的错误是因为没有菜单物业 MenusController 也就是说你没有任何声明名为菜单。它看起来像你试图使用菜单对象的MenuAddController,但它显然是因为它是一个完全不同的类中不存在MenusController

Your error is because there is no Menu property on MenusController i.e. you haven't declared anything called Menu. It looks like you're trying to use the Menu object on the MenuAddController, but it obviously doesn't exist in MenusController since it's an entirely different class.

在你的类,你可能不充分地把握MVC模式的名称。你的 MenuAddController 看起来像它试图成为一个视图模型和可能的资源库,以及 - 我不能肯定地说你的意图是什么。好消息是 MenusController 是几乎没有。它唯一缺少的是坚持/菜单(要添加的东西)保存到数据库的方式。

By the names of your classes, you might not fully be grasping the MVC pattern. Your MenuAddController looks like it's attempting to be a view model and possibly a repository as well--I can't say for certain what your intention is. The good news is the MenusController is just about there. The only thing it's missing is a way to persist/save the menu (the thing being added) to a database.

让我们从 MenusController 开始,因为你似乎是最接近完成那里。正如前面提到的数据访问是所有的缺失。我会考虑增加一个 BonTempsDbContext 属性控制器类,或者至少是实例化一个添加(POST)内动作例如

Let's start with MenusController, since you seem to be closest to completion there. As mentioned the data access is all that's missing. I'd consider adding a BonTempsDbContext property to the controller class, or at the very least instantiating one inside the Add (POST) action e.g.

public class MenusController
{
    private BonTempsDbContext db = new BonTempsDbContext();
    // actions and stuff
}

或行动本身例如

[HttpPost]
public ActionResult Add(SomeViewModel vm)
{
    var db = new BonTempsDbContext();
    // check model state, etc
        // db.Menus.Add(vm.Menu)
}

其他唯一的怪胎我看到的是GET添加动作。您的变量名称说明你传递一个控制器,这使得在MVC的上下文中没有任何意义。我最常看到的约定是一个空的视图模型对象(以下描述略)传递给安装程序的形式发布到行动的POST版本。

The only other oddity I see is the GET Add action. Your variable name suggests you're passing it a controller, which makes no sense in the context of MVC. The convention I see most often is to pass an empty view model object (described a little below) to setup the form to be posted to the POST version of the action.

这是不完整的(排序伪code的),因为你提到它的功课!

This is not complete (sort of pseudo code) since you mentioned it's homework!

现在到其他位。 MenuAddController没有任何意义(对我来说)作为一个对象或名称,让我们考虑一个名为 MenuAddViewModel 的再presents的的状态新对象要添加一个Menu对象的即对象的时候,用户提交表单(你没有显示。)ASP.NET MVC会为你绑定也许有上说,视图模型对象的菜单属性,这就是你再打算保存到数据库如:

Now onto the other bits. MenuAddController doesn't make any sense (to me) as an object or name, so let's consider a new object called MenuAddViewModel that represents the state of a Menu object to be added i.e. the object ASP.NET MVC will bind for you when the user submits the form (which you haven't shown.) Maybe there's a Menu property on said view model object and that's what you're planning on saving to the database e.g.

public class MenuAddViewModel
{
    public Menu Menu { get; set; }
    // ...
}

这可能是简单,如果以一种MVC可以自动将绑定菜单属性生成表单。这样,您就可以重复使用(几乎总是一件好事),在这两个你加入行动MenuAddViewModel对象。你通过它来获取视图,它可以帮助填充空的形式,然后在用户提交(邮政),以后的看法和噗,数据上下文可以将其保存到数据库或所到之处。

It might be that simple if your form is generated in a way that MVC can automagically bind the Menu property. This way, you can reuse (almost always a good thing) the MenuAddViewModel object in both your Add actions. You pass it to GET view and it helps populate an empty form, then the user submits (POST's) to the "post" view and POOF, the data context can save it to the database or wherever it goes.

我描述并不需要一个数据上下文对象,因为它只需要知道菜单对象的状态,新的视图模型对象。如果你需要的菜单项结合,可以添加到上述视图模型对象的属性,并从那里走了。

The new view model object I'm describing wouldn't need a data context object, since it only needs to know about the state of a Menu object. If you need to combine menu items, you could add properties to said view model object and go from there.

这篇关于从另一个模型的菜创建一个菜单,一个DropDownList一个创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆