Exemplo n.º 1
0
        public virtual ActionResult Edit(object id, int?tabFocus)
        {
            id = Server.UrlDecode(id.ToString());
            id = Convert.ChangeType(id, LinqToSqlUtils.GetPKPropertyInfo(
                                        MetaData.EntityType).PropertyType);
            var obj = Repository.GetByPK(id);

            if (obj == null)
            {
                return(Content("ќбъект не найден"));
            }

            var editVM = new EditVM(obj, MetaData);

            AddExtraControls(editVM, false);

            /*    if(tabFocus.HasValue)
             *  {
             *      for (int i = 0; i < editVM.MetaData.ExtraControls.Count; i++)
             *      {
             *          var control = editVM.MetaData.ExtraControls[i];
             *          if (control.DisplayName.GetHashCode() == tabFocus)
             *              editVM.TabFocus = i + 1;
             *      }
             *  }*/
            editVM.SiteUrl = Html.GetUrlFor(obj);

            SetSiteObject(id, editVM);

            return(View("Edit", editVM));
        }
Exemplo n.º 2
0
 public Entity(object entity)
 {
     Type = entity.GetType();
     if (LinqToSqlUtils.GetPKPropertyInfo(Type) != null)
     {
         PK = LinqToSqlUtils.GetPK(entity);
     }
     TypeName = Type.Name;
     if (entity is IEntityCommonInfo)
     {
         Name = entity.As <IEntityCommonInfo>().Name;
     }
 }
Exemplo n.º 3
0
        public virtual void UpdateOrInsert(T obj)
        {
            var id = LinqToSqlUtils.GetPK(obj);

            if (id.Equals(LinqToSqlUtils.GetPKPropertyInfo(_type).PropertyType.Default()))
            {
                this.InsertAndSubmit(obj);
                return;
            }
            var oldOjb = GetByPK(id);

            oldOjb.UpdateByMeta(obj);
            context.SubmitChanges();
        }
Exemplo n.º 4
0
        public static object GetObject(Type type, object id)
        {
            var constructor    = type.GetConstructors()[0];
            var obj            = constructor.Invoke(null);
            var publicPIs      = type.GetProperties();
            var associationPIs = publicPIs.Where(pi => pi.HasAttribute(typeof(AssociationAttribute)));
            var thisKeys       = associationPIs.Select(pi => pi.GetAttribute <AssociationAttribute>().ThisKey);
            var idPropertyname = LinqToSqlUtils.GetPKPropertyName(type);

            foreach (var propertyInfo in publicPIs)
            {
                object value = null;
                if (propertyInfo.Name == idPropertyname)
                {
                    value = Convert.ChangeType(id, propertyInfo.PropertyType);
                }
                else if (thisKeys.Contains(propertyInfo.Name))
                {
                    continue;
                }
                else if (propertyInfo.PropertyType == typeof(string))
                {
                    value = propertyInfo.Name + id;
                }

                if (propertyInfo.CanWrite)
                {
                    propertyInfo.SetValue(obj, value, new object[0]);
                }
            }

            var foreignKeyPIs = associationPIs.Where(pi => pi.GetAttribute <AssociationAttribute>().IsForeignKey);

            foreach (var propertyInfo in foreignKeyPIs)
            {
                if (propertyInfo.PropertyType == type)
                {
                    continue;
                }
                var idPI = LinqToSqlUtils.GetPKPropertyInfo(propertyInfo.PropertyType);
                if (propertyInfo.CanWrite)
                {
                    propertyInfo.SetValue(obj, GetObject(propertyInfo.PropertyType,
                                                         GetDefaultId(idPI.PropertyType)), new object[0]);
                }
            }

            return(obj);
        }
Exemplo n.º 5
0
        public static List <SelectListItem> GetSelectItemList <T>(
            IEnumerable <T> source)
        {
            var result        = new List <SelectListItem>();
            var displayColumn =
                Config.DescriptionProvider.GetAttribute <DisplayColumnAttribute>(typeof(T))
                .DisplayColumn;
            var idProperty = LinqToSqlUtils.GetPKPropertyInfo(typeof(T));

            foreach (var item in source)
            {
                result.Add(new SelectListItem
                {
                    Text  = item.GetValue(displayColumn).ToString(),
                    Value = idProperty.GetValue(item).ToString()
                });
            }
            return(result);
        }