コード例 #1
0
        public ActionResult Update(ListItemModel model)
        {
            if (!CheckPermission(ListsPermissions.ManageLists))
            {
                return(new HttpUnauthorizedResult());
            }

            var listItem = model.Id != 0
                ? listItemService.GetById(model.Id)
                : new ListItem();

            listItem.Title           = model.Title;
            listItem.PictureUrl      = model.PictureUrl;
            listItem.Position        = model.Position;
            listItem.Enabled         = model.Enabled;
            listItem.MetaKeywords    = model.MetaKeywords;
            listItem.MetaDescription = model.MetaDescription;

            if (!string.IsNullOrEmpty(model.Slug))
            {
                listItem.Slug = model.Slug.Trim(' ', '/', '.', '~');
            }

            if (string.IsNullOrEmpty(listItem.Slug))
            {
                listItem.Slug = model.Title.ToSlugUrl();
            }

            if (model.Id == 0)
            {
                listItem.CreatedDate  = DateTime.UtcNow;
                listItem.ModifiedDate = listItem.CreatedDate;
                listItem.ListId       = model.ListId;
            }
            else
            {
                listItem.ModifiedDate = DateTime.UtcNow;
            }

            var fields = listFieldService.GetFields(listItem.ListId);

            var values = fields.ToDictionary(field => field.Name, field => field.GetControlFormValue(this, WorkContext));

            listItem.Values = values.SharpSerialize();

            listItemService.Save(listItem);

            // Save categories
            if (model.Id != 0)
            {
                listItemService.RemoveCategories(model.Id);
            }

            if (model.Categories != null && model.Categories.Length > 0)
            {
                listItemService.AddCategories(listItem.Id, model.Categories);
            }

            return(new AjaxResult().Redirect(Url.Action("Index", "ListItem", new { listId = model.ListId })));
        }