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

4 posts, 1 answers
  1. Richard
    Richard avatar
    139 posts
    Member since:
    Feb 2012

    Posted 09 Nov Link to this post

    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 ?

  2. Neli
    Admin
    Neli avatar
    59 posts

    Posted 13 Nov Link to this post

    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.
  3. Richard
    Richard avatar
    139 posts
    Member since:
    Feb 2012

    Posted 13 Nov Link to this post

    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.

  4. Answer
    Neli
    Admin
    Neli avatar
    59 posts

    Posted 14 Nov Link to this post

    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.
Back to Top