How to screen view model in MVC 5

I am trying to work on a simple application. I have three SQL tables contributed through the Entity Framework, and models were automatically created. I want Visual Studio to be able to automatically create Create / Details / Edit images, etc. I can do this automatically when I am in the same model (for example, as a name), but cannot get anywhere when using View Model as a source.

Here are my models

Name

public partial class Name { public Name() { this.Addresses = new HashSet<Address>(); this.Emails = new HashSet<Email>(); } public int ID { get; set; } public string FIRST_NAME { get; set; } public string LAST_NAME { get; set; } public virtual ICollection<Address> Addresses { get; set; } public virtual ICollection<Email> Emails { get; set; } } 

Address

 public partial class Address { public int ADDRESS_ID { get; set; } public int NameID { get; set; } public string ADDRESS_1 { get; set; } public string CITY { get; set; } public string STATE { get; set; } public string ZIP { get; set; } public virtual Name Name { get; set; } } 

Email

 public partial class Email { public int EMAIL_ID { get; set; } public int NameID { get; set; } public string EMAIL { get; set; } public virtual Name Name { get; set; } } 

and View Model I created from all three

 public class MainVM { public Name Name { get; set; } public Address Address { get; set; } public Email Email { get; set; } } 

I can go through the steps to create a controller - Right-click Controllers -> Add -> Controller -> MVC 5 Controller with Views using Entity Framework.

Next I will get to this screen.

enter image description here

If I click Add, I will get the following error.

enter image description here

I read in other answers that you need to clear the data context class (from the first image) if you are using the View Model, but if I do, the Add button will be deactivated. I can’t move on. Any ideas here?

+6
source share
3 answers

I am sure that the original poster still cannot find the answer by this time. but he can help seekers like me ..

This article has been discovered. Link

It seems that when going through the route Controller β†’ Add β†’ New forest item β†’ MVC-controller with views, using the Entity Framework , does not work very well with view models.

If you did not provide the DataContext class in the aforementioned scaffolding process when choosing your view model, MVC scaffolding will not let you continue. As you indicated, the Add button is disabled.

The workaround is to take a two-step approach.

First create controller actions using scaffolding ( Controllers β†’ Add β†’ New Lining Element β†’ MVC Controller with Read / Write Actions )

Then add views by right-clicking on individual controller action methods, and then using the forests. (Controller action method β†’ ​​Right-click β†’ Add View β†’ Template β†’ [select anything, but Empty (without model)] β†’ Model class β†’ [select your viewing model here] β†’ Leave the data context field blank β†’ The add button will be enabled) .

The related article contains detailed instructions, please take a look.

However, you still need to add the code yourself to work with the database using the Entity infrastructure in the controller’s action methods. (Or you can choose to enter layers, repositories, etc. YMMV ). But it helps to avoid writing a lot of code to create your views.

PS: I found that this approach works fine for me using ASP.Net core 1.1

+3
source

I tried using the View Model in the controller and got the same error. Then I added an identification key to solve the error, and EF created a table representing the view model in my database. At the same time, I came to the conclusion that View Models do not know anything about the database and are used only for presentation in the view. In the controller, I handled database operations for different objects.

I needed to create the view manually and use @model myproject.ViewModels.MyViewModel in the view to represent the View model for html helpers.

This process worked for me, and I was able to save the information in the appropriate database tables for the entity model.

The following is sample code. You can use the matching tool instead of manually entering the code. Breaking viewmodel in essence for saving using Entity Framework.

  public ActionResult EmployeeDepartment(EmployeeViewModel evm) { if(ModelState.IsValid) { try { Department dept = new Department(); dept.DepartmentName = evm.DepartmentName; dept.DepartmentNumber = evm.DepartmentNumber; db.Departments.Add(dept); db.SaveChanges(); Employee emp = new Employee(); emp.FirstName = evm.FirstName; emp.LastName = evm.LastName; emp.Department = dept; db.Employees.Add(emp); db.SaveChanges(); return RedirectToAction("Index"); } catch(Exception ex) { //write code for exception } } return View(); } 
+2
source

Save the data context class, and then try to add the view. It worked for me. Also, add [Key] to any id attribute.

0
source

Source: https://habr.com/ru/post/985800/


All Articles