Ejemplo n.º 1
0
        public Presenter(Abstractions.IModel model, Abstractions.IView view)
        {
            this.model = model;
            this.view  = view;

            this.view.OnTextChange += Display;
        }
Ejemplo n.º 2
0
        public async Task <IHtmlContent> InvokeAsync(Abstractions.IView view)
        {
            // We always need a view name to invoke
            if (string.IsNullOrEmpty(view.ViewName))
            {
                throw new ArgumentNullException(nameof(view.ViewName));
            }

            var result = FindView(view.ViewName);

            if (!result.Success)
            {
                throw new Exception($"A view with the name \"{view.ViewName}\" could not be found!");
            }

            // Log the invocation
            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"Attempting to invoke partial view \"{view.ViewName}\".");
            }

            var builder = new HtmlContentBuilder();

            using (var writer = new StringWriter())
            {
                // Render view
                await RenderPartialViewAsync(writer, view.Model, ViewContext.ViewData, result.View);

                // Write results
                builder.WriteTo(writer, HtmlEncoder.Default);
                // Return builder
                return(builder.SetHtmlContent(writer.ToString()));
            }
        }
Ejemplo n.º 3
0
        public async Task <IHtmlContent> InvokeAsync(Abstractions.IView view)
        {
            // We always need a view name to invoke
            if (string.IsNullOrEmpty(view.ViewName))
            {
                throw new ArgumentNullException(nameof(view.ViewName));
            }

            if (!(_viewComponentHelper is DefaultViewComponentHelper helper))
            {
                throw new ArgumentNullException(
                          $"{_viewComponentHelper.GetType()} cannot be converted to DefaultViewComponentHelper");
            }

            // Contextualize view component
            helper.Contextualize(ViewContext);

            // Log the invocation
            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"Attempting to invoke view component \"{view.ViewName}\".");
            }

            try
            {
                return(await _viewComponentHelper.InvokeAsync(view.ViewName, view.Model));
            }
            catch (Exception e)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                {
                    _logger.LogError(e,
                                     $"An exception occurred whilst invoking the view component with name \"{view.ViewName}\". {e.Message}");
                }
                throw;
            }
        }