/// <summary> /// Attempts MVC model binding on the request variables. Respects custom binders. /// </summary> /// <param name="model">Model to bind to</param> /// <param name="controllerContext">The current controller context, if any (optional) - used to persist ModelState validation</param> /// <returns>True if the model passes all validations</returns> protected bool TryUpdateModel(TModel model, ControllerContext controllerContext) { if (model == null) { throw new ArgumentNullException("model"); } if (controllerContext == null) { controllerContext = ViewRenderer.CreateController <EmptyController>().ControllerContext; } Predicate <string> propertyFilter = propertyName => true; var bindAttribute = typeof(TModel).GetCustomAttributes(typeof(BindAttribute), true).FirstOrDefault() as BindAttribute; if (bindAttribute != null) { propertyFilter = bindAttribute.IsPropertyAllowed; } IModelBinder binder = Binders.GetBinder(typeof(TModel)); var bindingContext = new ModelBindingContext { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(TModel)), ModelName = null, ModelState = ((Controller)controllerContext.Controller).ModelState, PropertyFilter = propertyFilter, ValueProvider = GetValueProvider(controllerContext) }; binder.BindModel(controllerContext, bindingContext); return(bindingContext.ModelState.IsValid); }
/// <summary> /// Renders the view. /// </summary> /// <param name="StatusCode">The status code.</param> /// <returns></returns> private string RenderView(int StatusCode) { var technicalContactEmail = System.Configuration.ConfigurationManager.AppSettings["TechnicalContactEmail"]; var ErrorList = new List <HttpErrorPage>(); ErrorList.Add(new HttpErrorPage() { ErrorCode = 400, ErrorTitle = "Bad Request ", ErrorDescription = "The server cannot process the request due to something that is perceived to be a client error.", TechnicalContact = technicalContactEmail }); ErrorList.Add(new HttpErrorPage() { ErrorCode = 401, ErrorTitle = "Unauthorized", ErrorDescription = "The requested resource requires authentication.", TechnicalContact = technicalContactEmail }); ErrorList.Add(new HttpErrorPage() { ErrorCode = 500, ErrorTitle = "Webservice currently unavailable ", ErrorDescription = "An unexpected condition was encountered.Our service team has been dispatched to bring it back online.", TechnicalContact = technicalContactEmail }); var response = HttpContext.Current.Response; var z = ErrorList.Where(x => x.ErrorCode == StatusCode).FirstOrDefault(); if (z == null) { z = new HttpErrorPage() { ErrorCode = StatusCode, ErrorTitle = "Unknown Error", ErrorDescription = "An unknown error has occured.", TechnicalContact = technicalContactEmail }; } // Create an arbitrary controller instance var controller = ViewRenderer.CreateController <FakeController>(); string html = ViewRenderer.RenderView( string.Format("~/views/shared/HttpErrorPages/Error.cshtml", StatusCode.ToString()), z, controller.ControllerContext); return(html); }
private string BuildContent(IPage page, List <Comment> comments, StringBuilder webpartHeaders, ControllerContext context, bool isEditState, bool isPublic) { var statusString = _context.Parameters.Length > 1 ? _context.Parameters[1].Trim() : "open"; var status = statusMapping.ContainsKey(statusString) ? statusMapping[statusString] : true; if (!page.Commentable) { return(""); } if (context == null) { var controller = ViewRenderer.CreateController <GenericController>(); context = controller.ControllerContext; } else { if (context.Controller.TempData["ViewData"] != null) { context.Controller.ViewData = (ViewDataDictionary)context.Controller.TempData["ViewData"]; } } var account = context.HttpContext.User.Identity.Name.ToLower(); var commentModel = new CommentViewModel { PageId = page.Id, Url = page.Url, Comments = comments, Status = status, IsEditState = isEditState, Email = GuessEmail(account), Name = GuessName(account).ToUpper() }; context.Controller.ViewBag.RecaptchaSkipValidation = _skipRecaptcha; var contentString = ViewRenderer.RenderViewToString(context, "~/Areas/Comment/views/Comment/_CommentPartial.cshtml", commentModel, true); return(contentString); }
public RazorViewShim() { ControllerContext = ViewRenderer.CreateController <EmptyController>().ControllerContext; }