Monday, June 17, 2013

What is closure in Javascript by exmaple code?

var variable = "top-level";
function parentFunction() {
  var variable = "local";
  function childFunction() {
    print(variable);
  }
  childFunction();
}
parentFunction();

Closure:
A Javascript function is defined inside another function, its local environment will be based on the local environment that surrounds it instead of the top-level environment.

Reference:
http://eloquentjavascript.net/chapter3.html

Friday, June 14, 2013

How to export html table into Excel file in ASP.MVC?

        public ActionResult ExportToExcel(int id)
        {
            string sb = "html table tag in here";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
            return File(buffer, "application/vnd.ms-excel");
        }

How to render a view into a string in ASP.NET MVC?


        protected string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString("action");

            ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }
        }