Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have several classes (entities) and a View Model. I want to edit my view model and save into each entity.

First: Is there something wrong with the model?
Second: How to retreive myViewModel data into the edit view? I couldn't find an Edit example suitable for my model.

What I have tried:

I have the following classes: SALES, PRICE and COUNTRIES where SALES gives one->many to the other classes.

C#
     public partial class SALES
      {
        [key]
        public int id_sales { get; set; }
        public DateTime offer_dateC { get; set; }
        public int comnamC { get; set; }
        public string sales_contactC { get; set; }
        public string prod_name_pharma_formC { get; set; }
        public virtual ICollection<PRICE> PRICE { get; set; }
        public virtual ICollection<COUNTRIES> COUNTRIES { get; set; 
      }

public partial class PRICE
    {
        [Key]
        public int id_price { get; set; }
        public decimal priceC { get; set; }
        public string strengthC { get; set; }
        public string quantity { get; set; }
        public string currency { get; set; }
        public virtual SALES SALES { get; set; }
    }

public partial class COUNTRIES
    {
        [Key]
        public int id_country { get; set; }
        public string countryC { get; set; }
        public string coountry_name { get; set; }
        public virtual SALES SALES { get; set; }
    }

And the ModelView is:
C#
public class myViewModel
    {
      [Key]
      public int id_offers { get; set; }

      public DateTime offer_dateC { get; set; }
      public int comnamC { get; set; }
      public string sales_contactC { get; set; }
      public string prod_name_pharma_formC { get; set; }
      public List<decimal> priceC { get; set; }
      public string strengthC { get; set; }
      public string quantity { get; set; }
      public string currency { get; set; }
      public List<string> countryC { get; set; }
    }
Posted
Updated 14-Mar-18 7:11am
v3
Comments
F-ES Sitecore 14-Mar-18 8:31am    
You can't learn such a big technology from scratch using form posts. Get a book on MVC and go through it, or at least try some getting-started tutorials online. This one is pretty good and will answer many of the basic questions you have

MVC Music Store
TempoClick 14-Mar-18 8:40am    
I have already been through EF tutorials and I am able to do Create and Delete procedures with EF. I just couldn't find something suitable for my Model to edit records.
F-ES Sitecore 14-Mar-18 10:11am    
The link I gave you covers editing models.
TempoClick 15-Mar-18 6:31am    
The example doesn't mention editing multiple tables. They only edit "Album" table.
F-ES Sitecore 15-Mar-18 6:39am    
If all your data is in a single model then extract it to the table-specific models. So create a Price class and populate its properties from the relevant properties of myViewModel and get EF to update that Price object in the database.

Your viewmodel is lacking IDs though, so you might need to include things in your viewmodel like what the id_price is so that you can use that in the Price class you create so EF knows which record to update in the database. That method can leave you open to some attacks by malicious users who change the price id in your html, so you might want to re-read the original data from the database and work out the correct id_price to use.

1 solution

look into the package manager console command update-database
 
Share this answer
 
Comments
TempoClick 14-Mar-18 9:25am    
How could I use this command line to update my view model? How is it related to the edit view?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900