Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have created a model and i want to add data of table with a select and not a foreach.
THe model is :
namespace OAGEST.ViewModels.Sales
{
    public class Type_soViewModel
    {
        #region Properties
        /// <summary>
        /// Affecte ou obtient le code
        /// </summary>
        [Required(ErrorMessage = "Le code est obligatoire")]
        [StringLength(4, ErrorMessage = "La longueur est de 4 caractères maximum")]
        public string code { get; set; }

        /// <summary>
        /// Affecte ou obtient la description
        /// </summary>
        [Required(ErrorMessage = "La description est obligatoire")]
        [StringLength(50, ErrorMessage = "La longueur est de 50 caractères maximum")]
        public string description { get; set; }
        #endregion
        #region Constructors
        public Type_soViewModel()
        {

        }

        public Type_soViewModel(type_so type_so)
        {
            this.code = type_so.code;
            this.description = type_so.description;
        }

To add data, i use :
var db = new OAGESTEntities();

            Type_so = new List<Type_soViewModel>();
            foreach (var item in db.type_so)
            {
                Type_so.Add(new Type_soViewModel(item));
            }


What I have tried:

I tried this to add data but it's not working :
using (var entity = new OAGESTEntities())
            {
                Type_so = entity.type_so.ToList().Select(ts => new Type_soViewModel()).ToList();
            }

The lines are inserted but there are empty :
context	
-		Type_so	Count = 2	System.Collections.Generic.List<OAGEST.ViewModels.Sales.Type_soViewModel>
-		[0]	{OAGEST.ViewModels.Sales.Type_soViewModel}	OAGEST.ViewModels.Sales.Type_soViewModel
		code	null	string
		description	null	string
-		[1]	{OAGEST.ViewModels.Sales.Type_soViewModel}	OAGEST.ViewModels.Sales.Type_soViewModel
		code	null	string
		description	null	string
+		Raw View		

Can you help me ?
Posted
Comments
j snooze 30-May-17 17:38pm    
pretty sure you have to tell it which fields from "entity" go into the Type_so properties.

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