2

Into a asp.net core 1.0 (Full Framework 4.6.1), I needed to render a partial view to string, so based on https://gist.github.com/ahmad-moussawi/1643d703c11699a6a4046e57247b4d09, I created a RenderView:

public class ViewRender
{
    private IRazorViewEngine _viewEngine;
    private ITempDataProvider _tempDataProvider;
    private IServiceProvider _serviceProvider;

    public ViewRender(
        IRazorViewEngine viewEngine,
        ITempDataProvider tempDataProvider,
        IServiceProvider serviceProvider)
    {
        _viewEngine = viewEngine;
        _tempDataProvider = tempDataProvider;
        _serviceProvider = serviceProvider;
    }

    public string Render<TModel>(string view, TModel model)
    {
        var actionContext = GetActionContext();

        var viewEngineResult = _viewEngine.FindView(actionContext, view, false);

        if (!viewEngineResult.Success)
        {
            throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", view));
        }

        var viewResult = viewEngineResult.View;

        using (var output = new StringWriter())
        {
            var viewContext = new ViewContext(
                actionContext,
                viewResult,
                new ViewDataDictionary<TModel>(
                    metadataProvider: new EmptyModelMetadataProvider(),
                    modelState: new ModelStateDictionary())
                {
                    Model = model
                },
                new TempDataDictionary(
                    actionContext.HttpContext,
                    _tempDataProvider),
                output,
                new HtmlHelperOptions());

            viewContext.Vi
            viewResult.RenderAsync(viewContext).GetAwaiter().GetResult();

            return output.ToString();
        }
    }

    private ActionContext GetActionContext()
    {
        var httpContext = new DefaultHttpContext();
        httpContext.RequestServices = _serviceProvider;
        return new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
    }
}

however when Controller call the PartialView:

string render = _viewRender.Render("Directory/_DirectoryData", directory);

Data set into ViewBag Controller previously as follows.

ViewBag.ParticipantTypesList = _dbInstance.ParticipantTypes;
ViewBag.DirectoryList = _dbInstance.Directories;
ViewBag.TaxCategoryList = _dbInstance.TaxCategories;

Doesn't exists when using them for combobox ítems:

<label asp-for="DirectoryItem_DirectoryItem" class="form-control-label"></label>
<select asp-for="DirectoryItem_DirectoryItem" asp-items="@(new SelectList(ViewBag.DirectoryList, "Id", "DirectoryId", null))" class="form-control">
  <option>--- Seleccione ---</option>
</select>

And Render throws an exception. Please help me how to get or transfer ViewBag.

¿I have to use _viewEngine.FindView(...) or _viewEngine.FindPage(...)?

Thanks

1 Answer 1

1

I've found answer by myself!

I not need to render partial view to a string which was causing me a headache. Just using return PartialView into Controller returns a string to the ajax function caller in javascript and therefore ViewBag variables can be accessed without problems into cshtml partial view.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.