This is a migrated thread and some comments may be shown as answers.

Render partial view to string with grid

3 Answers 344 Views
Grid
This is a migrated thread and some comments may be shown as answers.
nicolas
Top achievements
Rank 1
nicolas asked on 17 Nov 2015, 09:54 AM

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);

3 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 19 Nov 2015, 09:05 AM

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
0
nicolas
Top achievements
Rank 1
answered on 25 Nov 2015, 11:04 AM

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,

0
Nikolay Rusev
Telerik team
answered on 27 Nov 2015, 07:03 AM

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
Tags
Grid
Asked by
nicolas
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
nicolas
Top achievements
Rank 1
Share this question
or