public ActionResult GetCourses(DtoCurriculumSearch search)
        {
            CourseBll bll = new CourseBll();
            IList <DtoCourseListItem> entities = bll.GetManageCourses(search);

            PropertyNamePrefixAction      action = PropertyNamePrefixAction.Remove;
            IEnumerable <CourseViewModel> list   =
                entities.Select(s => s.ConvertTo <CourseViewModel>(action));

            return(Table(list, search.Pagination.TotalCount));
        }
Ejemplo n.º 2
0
        public static T ConvertTo <T>(
            this object o,
            PropertyNamePrefixAction action)
        {
            Check.IfNull(o, nameof(o));

            JsonSerializerSettings settings = null;

            Type type =
                action == PropertyNamePrefixAction.Add ? o.GetType() : typeof(T);

            if (type.IsDefined(typeof(JsonSerializeWithPrefixAttribute), true))
            {
                JsonSerializeWithPrefixAttribute attribute =
                    type.GetCustomAttribute <JsonSerializeWithPrefixAttribute>();
                string prefix = attribute.GetPrefix();

                DefaultContractResolver contractResolver = null;
                if (action == PropertyNamePrefixAction.Add)
                {
                    contractResolver = new AddPrefixContractResolver(prefix);
                }
                else
                {
                    contractResolver = new RemovePrefixContractResolver(prefix);
                }
                settings = new JsonSerializerSettings();
                //在序列化时,指定目标属性名字的格式
                settings.ContractResolver = contractResolver;
            }

            string jsonString = JsonConvert.SerializeObject(o, settings);
            T      result     = JsonConvert.DeserializeObject <T>(jsonString);

            return(result);
        }