Render partial view to string with grid

4 posts, 0 answers
  1. nicolas
    nicolas avatar
    7 posts
    Member since:
    Mar 2015

    Posted 17 Nov 2015 Link to this post

    Hi,

    I have to render my partial view in html string (in controller), to return json object.

    I found this function that works very well :

    01.public static String RenderViewToString(ControllerContext context, String viewPath, object model = null)
    02.        {
    03.            context.Controller.ViewData.Model = model;
    04.            using (var sw = new StringWriter())
    05.            {
    06.                //var viewResult = ViewEngines.Engines.FindView(context, viewPath, null);
    07.                var viewResult = ViewEngines.Engines.FindPartialView(context, viewPath);
    08.                var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw);
    09.                viewResult.View.Render(viewContext, sw);
    10.                viewResult.ViewEngine.ReleaseView(context, viewResult.View);
    11.                return sw.GetStringBuilder().ToString();
    12.            }
    13.        }

     

    With a kendo grid in partial view, i'm getting a "System.NullReferenceException" on the declaration  @(Html.Kendo().Grid(.....

    If I return the view with a "Return view" in my controller, it's ok.

     

    My grid:

    01.@(Html.Kendo().Grid((IEnumerable<MAGI.Model.DigicodeImmeuble>)mvDigicode)
    02.        .Name("gridSyntheseDigicode")
    03.        .HtmlAttributes(new { style = "width:100%;height: auto;", @class = "k-grid_encart k-grid_nopage" })
    04.        .Columns(t =>
    05.            {
    06.                t.Bound(c => c.Digicode1.Nom).Title(tabTraduction[701972] + " 1");
    07.                t.Bound(c => c.Digicode1.NoDigicode).Title(tabTraduction[100620]);
    08.                t.Bound(c => c.Digicode2.Nom).Title(tabTraduction[701972] + " 2");
    09.                t.Bound(c => c.Digicode2.NoDigicode).Title(tabTraduction[100620]);
    10.            }
    11.        )
    12.        .Sortable()
    13.        .Selectable()
    14.        .DataSource(dataSource => dataSource
    15.            .Ajax()
    16.            .ServerOperation(false)
    17.        )
    18.        .Reorderable(reorder => reorder.Columns(true))
    19.        .Events(events => events.DataBound("onDataBoundGridStandard"))
    20.    )

     

    mvDigicode and tabTraduction are not null. 

     

    That's work with the kendo chart module.

    What's wrong with the grid ?

     

    Many thanks, 

     


     
    06.                var viewResult = ViewEngines.Engines.FindPartialView(context, viewPath);
  2. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 19 Nov 2015 Link to this post

    Hello Nicolas,

     

    We are not sure what might cause this error in the described scenario. We will need a isolated runnable example in order to assist you further.

     

    Regards,
    Nikolay Rusev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. nicolas
    nicolas avatar
    7 posts
    Member since:
    Mar 2015

    Posted 25 Nov 2015 in reply to Nikolay Rusev Link to this post

    Hello Nikolay,

    When I created the isolated example, i worked great.
    So I searched differences between the isolated and my application, and found that this is the Task object in controller that causes the error.

    Maybe the "await Task.WhenAll()" is not completed when I want to render the view ?

    When I render my view, I don't need the task so it's "resolved". But if you still want a isolated example, I can send you it.

    Regards,

  5. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 27 Nov 2015 Link to this post

    Hello Nicolas,

     

    I'm not sure how the Task is used in this case. Does this scenario work with other MVC widgets?

    We will need a runnable example of the scenario showing the exception.

     

    Looking forward to hearing from you.

     

    Regards,
    Nikolay Rusev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready