Thanks to Mike's article "
Exporting The Razor WebGrid To Excel Using OleDb" and his guidance here, I was able to find a solution.
My objective was to export an excel file based on a Webmatrix Webgrid using the methods described in the article above. However, I needed to pass a variable through a URL Querystring to help me limit the results in the Excel export to a specific user.
The URL of the origination page, as mentioned above, looks like this:
.../WebPage.cshtml?USER=jsmith&TYPE=Analyst
I used this jQuery code to pass information on to the "GenerateExcel.cshtml" file:
Code:
/* Get URL QueryString Parameters*/
(function($) {
$.QueryString = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'))
})(jQuery);
/* Pass request on to GenerateExcel file */
$(function () {
$('#excel').on('hover', function () {
$(this).css('cursor', 'pointer');
});
$('#excel').on('click', function () {
var user = $.QueryString["USER"];
var type = $.QueryString["TYPE"];
$('<iframe src="/GenerateExcel?User=' + user + '&Type=' + type + '"></iframe>').appendTo('body').hide();
});
});
Note the use of single and double quotes in the iframe URL.
Also, the jQuery that splits the URL Querystring was found at this
StackOverflow post.
Using the above jQuery in tandem with Mike's code from the article mentioned above, I am now able to export an excel file using specific parameters defined in the URL Querystring.
Regards,
Joel