protected ActionResult CreateActionResultBasedOnAjaxRequest(dynamic model, string partialView, string mainView, string returnUrl)
        {
            bool isAjaxRequest = Request.IsAjaxRequest();

            if (isAjaxRequest)
            {
                AjaxMessageViewModel ajaxMessageModel = new AjaxMessageViewModel {
                    IsDone = true
                };
                ajaxMessageModel.Data = JsonConvert.SerializeObject(model);

                if (!ModelState.IsValid)
                {
                    foreach (var valueModel in ModelState)
                    {
                        foreach (var errorModel in valueModel.Value.Errors)
                        {
                            string error = errorModel.Exception != null ? errorModel.Exception.Message : errorModel.ErrorMessage;
                            ajaxMessageModel.Errors.Add(new KeyValuePair <string, string>(valueModel.Key, error));
                        }
                    }
                }

                if (model != null)
                {
                    this.widgetService.GetWidgets(model, this.HttpContext);
                }

                if (!string.IsNullOrEmpty(partialView))
                {
                    ajaxMessageModel.Html = CRMHelper.RenderPartialViewToString(this, partialView, model);
                }

                return(this.Json(ajaxMessageModel, JsonRequestBehavior.AllowGet));
            }
            else
            {
                if (!string.IsNullOrEmpty(returnUrl))
                {
                    return(this.Redirect(returnUrl));
                }
                else if (string.IsNullOrEmpty(mainView))
                {
                    // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
                    return(this.View((object)model));
                }
                else
                {
                    return(this.View(mainView, (object)model));
                }
            }
        }
        public virtual ActionResult Edit(int[] ids, string returnUrl)
        {
            var model = this.CreateEditPermissionsModel(ids);

            model.ReturnUrl = returnUrl;

            if (Request.IsAjaxRequest())
            {
                AjaxMessageViewModel ajaxMessageModel = new AjaxMessageViewModel {
                    IsDone = true
                };
                this.widgetService.GetWidgets(model, this.HttpContext);
                ajaxMessageModel.Html = CRMHelper.RenderPartialViewToString(this, "Edit", model);

                return(this.Json(ajaxMessageModel, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(View(model));
            }
        }
        public ActionResult UploadFile(Guid guid, string qqFile, string contentTypeForFile)
        {
            if ((!_services.Authorizer.Authorize(Permissions.CustomerPermission) &&
                 !_services.Authorizer.Authorize(Permissions.OperatorPermission)) || guid == Guid.Empty)
            {
                return(Json(new { success = false, message = T("You are not permitted to upload files").Text }));
            }

            var stream = Request.InputStream;

            if (string.IsNullOrEmpty(Request["qqFile"]))
            {
                var postedFile = Request.Files[0];
                stream = postedFile.InputStream;
                qqFile = postedFile.FileName;
            }

            Dictionary <string, string> errors = new Dictionary <string, string>();
            int filesCount;

            if (!this.fileService.AddFile(qqFile, stream, contentTypeForFile, guid, errors, out filesCount))
            {
                return(Json(new { success = false, Errors = errors, message = errors.Count > 0 ? errors.First().Value : string.Empty }));
            }

            RouteValueDictionary routeValueDictionary = new RouteValueDictionary();

            routeValueDictionary.Add("guid", guid);
            routeValueDictionary.Add("area", "orchard.crm.core");
            routeValueDictionary.Add("name", qqFile);

            RouteValueDictionary activityStreamRouteValues = new RouteValueDictionary();

            activityStreamRouteValues.Add("action", "Display");
            activityStreamRouteValues.Add("controller", "Item");
            activityStreamRouteValues.Add("area", "orchard.crm.core");

            // Add to activity stream
            var fileRecord = fileRepository.Table.FirstOrDefault(c => c.FolderGuid == guid);

            if (fileRecord != null)
            {
                var item   = _services.ContentManager.Get(fileRecord.ContentItemRecord.Id);
                int userId = _services.WorkContext.CurrentUser.Id;
                fileRecord.FilesCount = filesCount + 1;
                fileRepository.Flush();

                activityStreamRouteValues.Add("id", item.Id);

                string changeDescription = string.Format(CultureInfo.CurrentUICulture, "Add a new file '{0}' to '{1}'", qqFile, this.GetContentItemDescriptionForActivityStream(item));
                this.activityStreamService.WriteChangesToStreamActivity(userId, item.Id, item.VersionRecord.Id, new ActivityStreamChangeItem[] { }, changeDescription, activityStreamRouteValues);
            }

            FileDisplayViewModel fileDisplayModel = new FileDisplayViewModel
            {
                Name        = qqFile,
                Uploaded    = DateTime.UtcNow,
                RouteValues = routeValueDictionary
            };

            dynamic model = new System.Dynamic.ExpandoObject();

            model.Model = fileDisplayModel;
            model.CurrentUserHasEditAccess = true;
            string fileLinkHtml = CRMHelper.RenderPartialViewToString(this, "FileLinkPartial", model);

            return(Json(new { success = true, FileLinkHtml = fileLinkHtml }, "text/html"));
        }