Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

MVC parse ajax to javascript excel

$
0
0

Based on this article i’ve created an Excel TaskPane App

The controller

using DatabaseWeb.Models;
using System.Web.Mvc;

namespace DatabaseWeb.Home
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult GetEmployee()
        {
            Employee john = new Employee
            {
                Id = 1,
                FirstName = "John",
                LastName = "Smith"
            };

            return Json(new { employee = john }, JsonRequestBehavior.AllowGet);
        }
    }
}

The model

namespace DatabaseWeb.Models
{
    public class Employee
    {

        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
 
    }
}

JAVASCRIPT

$(document).ready(function () {$('p#getEmployee').click(function () {
        GetEmployeeUsingAjax();
    });
});

function GetEmployeeUsingAjax() {
    $.ajax({
        type: 'GET',
        url: 'Home/GetEmployee',
        success: function (emp) {$('#id').text(emp.employee.Id);$('#firstName').text(emp.employee.FirstName);$('#lastName').text(emp.employee.LastName);
        },
        error: function (emp) {
            //alert('error');
            return "error";
        }
    });
}

body home.html

<div id="id"></div><div id="firstName"></div><div id="lastName"></div><p id="getEmployee">Get Employee</p>

When I run the project it generate the id, firstname and lastname in my task pane. GREAT! So my next question is very simple. How can I generate the lastname in Excel (home.js)

function loadSampleData() {

var values = GetEmployeeUsingAjax(lastname);

Excel.run(function (ctx) {
       
     	var sheet = ctx.workbook.worksheets.getActiveWorksheet();
       
     	sheet.getRange("A1").values = values;
     return ctx.sync();
        })
        .catch(errorHandler);
}

The above code won't work. 

What is the exact syntax? The follow code show my intensions but does not work.

var values = GetEmployeeUsingAjax(lastname);


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>