BCL classes not found when rendering view in ASP.NET Core 1
Date : March 29 2020, 07:55 AM
this will help To use the latest MVC RC2 nightly builds with DNX, you must reference a compatibility package named Microsoft.AspNetCore.Mvc.Dnx and call services.AddMvcDnx() from your ConfigureServices() method. Read https://github.com/aspnet/Announcements/issues/154 for more information.
|
How to check if view component exists before rendering in razor view in ASP.NET Core
Date : March 29 2020, 07:55 AM
may help you . You can inject IViewComponentSelector into your view to check if component exists: @inject Microsoft.AspNetCore.Mvc.ViewComponents.IViewComponentSelector selector
@if (selector.SelectComponent("MyComponent")!= null)
{
@await Component.InvokeAsync("MyComponent", new { data = 1 })
}
else
{
<p>Component not found</p>
}
|
MVC Core - strange view rendering issue
Date : March 29 2020, 07:55 AM
this will help MVC stores the posted back values in ModelState. These values are used by default in @Html helpers - as a convenience. This allows the values of hidden form fields to be preserved through postbacks, even if they don't have properties in the view-model. [HttpPost]
public IActionResult ConfirmDetails(CreateSaleViewModel model)
{
var viewModel = new ConfirmDetailsViewModel
{
ConfirmOrderId = model.OrderId,
...
};
ModelState.Clear(); // force asp-helpers to use the updated model's values
return View("ConfirmDetails", viewModel);
}
|
Rendering Partial View to String in Asp.net Core 2.2
Tag : chash , By : abuiles
Date : March 29 2020, 07:55 AM
Does that help Why Json result with html string? You can return a partial view directly to return html. public IActionResult GetUpsertPartialView(MessageBoard messageBoard)
{
return PartialView("someviewname", messageBoard);
}
|
Rendering view to string in core 3.0: Could not find an IRouter associated with the ActionContext
Tag : chash , By : Harvey
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Found a solution: I figured that since creating an ActionContext and providing it with an empty RouteData object was giving me an IRouter exception, the next best solution was to see if I could just use the HttpContext from the actual request. var htmlBody = await Renderer.RenderToString($"/Views/Shared/confirm-email.cshtml", model);
public class ViewRenderService : IViewRenderService
{
private readonly IRazorViewEngine _razorViewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IHttpContextAccessor _contextAccessor;
public ViewRenderService(IRazorViewEngine razorViewEngine,
ITempDataProvider tempDataProvider,
IHttpContextAccessor contextAccessor)
{
_razorViewEngine = razorViewEngine;
_tempDataProvider = tempDataProvider;
_contextAccessor = contextAccessor;
}
public async Task<string> RenderToString(string viewName, object model)
{
var actionContext = new ActionContext(_contextAccessor.HttpContext, _contextAccessor.HttpContext.GetRouteData(), new ActionDescriptor());
await using var sw = new StringWriter();
var viewResult = FindView(actionContext, viewName);
if (viewResult == null)
{
throw new ArgumentNullException($"{viewName} does not match any available view");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
};
var viewContext = new ViewContext(
actionContext,
viewResult,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);
await viewResult.RenderAsync(viewContext);
return sw.ToString();
}
private IView FindView(ActionContext actionContext, string viewName)
{
var getViewResult = _razorViewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: true);
if (getViewResult.Success)
{
return getViewResult.View;
}
var findViewResult = _razorViewEngine.FindView(actionContext, viewName, isMainPage: true);
if (findViewResult.Success)
{
return findViewResult.View;
}
var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);
var errorMessage = string.Join(
Environment.NewLine,
new[] { $"Unable to find view '{viewName}'. The following locations were searched:" }.Concat(searchedLocations));
throw new InvalidOperationException(errorMessage);
}
}
|