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

View (Razor) code to render model that is server prepared workbook.ToJson()

3 Answers 148 Views
Spreadsheet
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 09 Nov 2017, 06:28 PM

The asp-net mvc example shows how to load a spreadsheet and serialize it.

public class HomeController : Controller
{
    public ActionResult Read()
    {
        var path = Server.MapPath("~/App_Data/path/to/document.xlsx");
        var workbook = Telerik.Web.Spreadsheet.Workbook.Load(path);
        //Uses Newtonsoft.Json internally to serialize fields correctly.
        return Content(workbook.ToJson(), Telerik.Web.Spreadsheet.MimeTypes.JSON);
    }
}

 

Question:

What would the razor code look like to render the serialized workbook sent as the view model ?

3 Answers, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 13 Nov 2017, 11:57 AM
Hello Richard,

The razor code for loading data from external file could be as follows:
@(Html.Kendo().Spreadsheet()
    .Name("spreadsheet")
)
 
<script>
    $(document).ready(function () {
        var spreadsheet = $("#spreadsheet").data("kendoSpreadsheet");
 
        $.getJSON("@Url.Action("Read", "Home")")
        .done(function (data) {
            spreadsheet.fromJSON(data);
        });
    });
</script>

The same code could be found on the cshtml tab on the 'Load Data from External File' example. For convenience, you may find a screenshot attached.


Regards,
Neli
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Richard
Top achievements
Rank 1
answered on 13 Nov 2017, 04:07 PM

Hi Neli:

I wanted to take things even closer to the controller.  The getJSON 'content' is actually the model for my page.  I have found using BindTo(Model) works for me.  I think similar code would be a useful demonstration in the MVC examples.

Controller: LabReportController.cs
 
  public ActionResult Preview(string lot_number) {
    var book = new Telerik.Windows.Documents.Spreadsheet.Model.Workbook();
    var sheet = book.Worksheets.Add();
    sheet.Name = model.ReportData.Lot;
    sheet.Cells[0, 0].SetValue("Report for " + lot_number);
 
    return View(Telerik.Web.Spreadsheet.Workbook.FromDocument(book));
  {
 
Viewer: Preview.cshtml
 
  @model Telerik.Web.Spreadsheet.Workbook
 
  @{
      ViewBag.Title = "Lab Report";
  }
 
  @(Html.Kendo().Spreadsheet().Name("report").BindTo(Model));
  

 

BindTo might be an obvious answer to a seasoned MVC developer, however I am a seasoned developer relatively new to MS ASP NET MVC.

0
Accepted
Neli
Telerik team
answered on 14 Nov 2017, 04:00 PM
Hello Richard,

I am glad you have found a solution for your scenario.

Please accept my apologies for sending you insufficient information and thank you very much for the feedback. 

Such example will be a good enhancement,  so it is going to be added to the Spreadsheet documentation.   

We are always trying to keep our documentation up-to-date and complete as much as possible. There are always articles that could be improved and additional content that needs to be created.  Suggestions for improving our documentation and Demos are always welcome.

When you have a suggestion for a new Demo or a feature, you could add it as an issue in our UserVoice Portal. This way other users will be able to cast their vote for it and increase its priority. 

Regards,
Neli
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Spreadsheet
Asked by
Richard
Top achievements
Rank 1
Answers by
Neli
Telerik team
Richard
Top achievements
Rank 1
Share this question
or