Create a menu view with a drop-down list from another dish model

I am a student and must apply for study. This is a reservation application for a restaurant. I want to create a Create view for a menu in which you can give the menu a name, and you choose which dishes this menu should have. These dishes are stored in a different model. I use this question: Create a drop-down list for MVC3 using Entity Framework (.edmx Model) and Razor Views && & && & Paste the database entry into multiple tables , but won't get it working. 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); } } 

He gives an error on

 Menu.Add(vm.Menu); 

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

Can anybody help me?

0
c # view asp.net-mvc-5 model
Nov 16 '14 at 13:09
source share
1 answer

Your error is related to the fact that MenusController does not have a Menu property, i.e. You have not announced anything called Menu . It looks like you are trying to use the Menu object in MenuAddController, but it clearly does not exist in MenusController, since this is a completely different class.

By the names of your classes, you may not fully grasp the MVC pattern. Your MenuAddController looks like it is trying to be a view model and possibly a repository - I can’t say exactly what your intention is. The good news is MenusController . The only thing that he lacks is a way to save / save the menu (adding things) to the database.

Let's start with the MenusController , as you seem to be closest to completion. As already mentioned, data access is all that is missing. I would like to add the BonTempsDbContext property to the controller class, or at least create an instance inside the Add (POST) action, for example.

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

or in the action itself, for example

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

The only other weirdness that I see is the GET Add action. Your variable name suggests that you pass it a controller, which does not make sense in the context of MVC. The congress that I see most often is to pass an empty view model object (described just below) to set up a form for publication in the POST version of the action.

This is not complete (like pseudo-code) as you mentioned homework!

Now for the rest of the bits. MenuAddController does not make any sense (for me) as an object or name, so consider a new object called MenuAddViewModel , which represents the state of the added Menu object, i.e. an ASP.NET MVC object will be bound for you when the user submits a form (which you did not show). Perhaps there is a Menu property on the specified object of the view model and what you plan to save in the database, for example

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

It can be that simple if your form is generated in such a way that MVC can automatically bind the Menu property. That way, you can reuse (almost always well) the MenuAddViewModel object in your Add actions. You pass it to the GET view, and it helps to fill in the blank form, then the user submits (POST) to the post and POOF views, the data context can save it in the database or wherever it is.

The new view model object that I am describing will not need a data context object because it only needs to know the state of the Menu object. If you need to combine menu items, you can add properties to the specified view model object and go from there.

+1
Nov 16 '14 at 16:59
source share



All Articles