MVC3 - posting byte array to a controller - Database RowVersion - asp.net-mvc
I am working on an MVC3 application. My client side ViewModel contains a SQL Server RowVersion property, which is a byte[]. It is rendered as an Object array on the client side. When I attempt to post my view model to a controller, the RowVersion property is always null.
I am assuming that the Controller serializer (JsonValueProviderFactory) is ignoring the Object array property.
I have seen this blog, however this does not apply, as I am posting JSON and not the form markup:
http://thedatafarm.com/blog/data-access/round-tripping-a-timestamp-field-with-ef4-1-code-first-and-mvc-3/
My view renders my viewmodel like so:
<script type="text/javascript">
var viewModel = #Html.Raw( Json.Encode( this.Model ) );
</script>
I then post the viewModel to the controller like so:
var data = {
'contact': viewModel
};
$.ajax({
type: 'POST',
url: '/Contact/Save',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
dataType: 'json',
success: function (data) {
// Success
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
Here is my action in the controller:
[HttpPost]
public JsonResult Save(Contact contact) {
return this.Json( this._contactService.Save( contact ) );
}
UPDATE: based on Darin's answer.
I was hoping for a cleaner solution, but since Darin provided the only answer, I will have to add a custom property that will serialize my byte[] "row_version" property to a Base64 string. And when the Base64 string is set to the new custom property, it converts the string back to a byte[]. Below is the custom "RowVersion" property that I added to my model:
public byte[] row_version {
get;
set;
}
public string RowVersion {
get {
if( this.row_version != null )
return Convert.ToBase64String( this.row_version );
return string.Empty;
}
set {
if( string.IsNullOrEmpty( value ) )
this.row_version = null;
else
this.row_version = Convert.FromBase64String( value );
}
}
My client side ViewModel contains a SQL Server RowVersion property, which is a byte[]
Make it so that instead of a byte[] your view model contains a string property which is the base64 representation of this byte[]. Then you won't have any problems roundtripping it to the client and back to the server where you will be able to get the original byte[] from the Base64 string.
Json.NET automatically encodes byte arrays as Base64.
You can use JsonNetResult instead of JsonResult:
from https://gist.github.com/DavidDeSloovere/5689824:
using System;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public class JsonNetResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/json";
if (this.ContentEncoding != null)
{
response.ContentEncoding = this.ContentEncoding;
}
if (this.Data == null)
{
return;
}
var jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
jsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var formatting = HttpContext.Current != null && HttpContext.Current.IsDebuggingEnabled ? Formatting.Indented : Formatting.None;
var serializedObject = JsonConvert.SerializeObject(Data, formatting, jsonSerializerSettings);
response.Write(serializedObject);
}
}
Usage:
[HttpPost]
public JsonResult Save(Contact contact) {
return new JsonNetResult { Data = _contactService.Save(contact) };
}
Related
How to bind posted data named "file[]" to an MVC model?
I am using Redactor as an HTML editor, which has a component for uploading images and files. Redactor takes care of the client side bit, and I need to provide the server side upload functionality. I have no problem getting the uploads to work if I use Request.Files in the controller. But I would like to bind the posted files to a Model, and I seem unable to do this, because the parameter they are sent with is files[] - with square brackets in the name. My question: Is it possible to bind the posted "file[]" to an MVC model? It's an invalid property name, and using file alone doesn't work. This file input looks like this. I can specify a name other than file, but Redactor adds [] to the end, regardless of the name. <input type="file" name="file" multiple="multiple" style="display: none;"> I am trying to bind to a property like this: public HttpPostedFileBase[] File { get; set; } When I watch the upload take place, I see this in the request (I presume that redactor may be adding the square brackets behind the scenes): Content-Disposition: form-data; name="file[]"; filename="my-image.jpg" Also relevant: Redactor always sends the uploading request with content-type as multipart/form-data. So you don't need to add this enctype anywhere
You should create a custom model binder to bind uploaded files to one property. First create a model with a HttpPostedFileBase[] property public class RactorModel { public HttpPostedFileBase[] Files { get; set; } } then implement DefaultModelBinder and override BindProperty public class RactorModelBinder : DefaultModelBinder { protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) { int len = controllerContext.HttpContext.Request.Files.AllKeys.Length; if (len > 0) { if (propertyDescriptor.PropertyType == typeof(HttpPostedFileBase[])) { string formName = string.Format("{0}[]", propertyDescriptor.Name); HttpPostedFileBase[] files = new HttpPostedFileBase[len]; for (int i = 0; i < len; i++) { files[i] = controllerContext.HttpContext.Request.Files[i]; } propertyDescriptor.SetValue(bindingContext.Model, files); return; } } base.BindProperty(controllerContext, bindingContext, propertyDescriptor); } } Also you should add binder provider to your project, then register it in global.asax public class RactorModenBinderProvider : IModelBinderProvider { public IModelBinder GetBinder(Type modelType) { if (modelType == typeof(RactorModel)) { return new RactorModelBinder(); } return null; } } ... ModelBinderProviders.BinderProviders.Insert(0, new RactorModenBinderProvider()); this isn't a general solution, but I guess you get the point.
I encountered similar problem during the integration of jQuery.filer in an ASP.NET MVC project. As jQuery.filer adds "[]" to the end of name attribute of input (i.e. from files to files[]), I had to change the value of name attribute manually as shown below: $('#FileUpload').attr('name', 'FileUpload'); Here is my approach used in some of project via AJAX and working without any problem. You might give a try and let me know if it works: ViewModel: [Display(Name = "Attachments")] [DataType(DataType.Upload)] public IEnumerable<HttpPostedFileBase> FileUpload { get; set; } View: #model ViewModel #using (Html.BeginForm("Insert", "Controller", FormMethod.Post, new { id = "frmCreate", enctype = "multipart/form-data" })) { #Html.TextBoxFor(m => m.FileUpload, new { type = "file", multiple = "multiple" }) <button id="btnSubmit" onclick="insert(event)" type="button">Save</button> } <script> function insert(event) { event.preventDefault(); //As jQuery.filer adds "[]" to the end of name attribute of input (i.e. from files to files[]) //we have to change the value of name attribute manually $('#FileUpload').attr('name', 'FileUpload'); var formdata = new FormData($('#frmCreate').get(0)); $.ajax({ type: "POST", url: '#Url.Action("Insert", "Cotroller")', cache: false, dataType: "json", data: formdata, /* If you are uploading files, then processData and contentType must be set to falsein order for FormData to work (otherwise comment out both of them) */ processData: false, contentType: false, success: function (response, textStatus, XMLHttpRequest) { //... } }); }; $(document).ready(function () { $('#FileUpload').filer({ //code omitted for brevity }); }); </script> Controller: public JsonResult Insert([Bind(Exclude = null)] ViewModel model) { if (ModelState.IsValid) { List<FileAttachment> fa = new List<FileAttachment>(); if (model.FileUpload != null) { FileAttachment fileAttachment = new FileAttachment //entity model { Created = DateTime.Now, FileMimeType = upload.ContentType, FileData = new byte[upload.ContentLength], FileName = upload.FileName, AuthorId = 1 }; upload.InputStream.Read(fileAttachment.FileData, 0, upload.ContentLength); fa.Add(fileAttachment); } //code omitted for brevity repository.SaveExperimentWithAttachment(model, fa); return Json(new { success = true, message = "Record has been created." }); } // If we got this far, something failed, redisplay form return Json(new { success = false, message = "Please check the form and try again." }); }
Troubleshoot MVC model binding failure - argument is null in controller
I am trying to POST an object from a WebJob to an MVC 4 controller. I am using Entity Framework. In the controller, I cannot get the object to bind properly (the argument is null). I have looked at many tutorials and it seems like my code should work. Model (does this need to be in a specific namespace for EF to find it?): public class CreateListingObject { public Listing listing; public List<GalleryImage> images; public CreateListingObject() { listing = new Listing(); images = new List<GalleryImage>(); } } public struct GalleryImage { public string picURL; public string caption; } POST: public void PostListing(CreateListingObject o) { Console.WriteLine("Posting listing: {0}", o.listing.Title); HttpClient _httpClient = new HttpClient(); Uri uri = new Uri(_serviceUri, "/Automaton/CreateTestListing"); string json = BizbotHelper.SerializeJson(o); HttpResponseMessage response = BizbotHelper.SendRequest(_httpClient, HttpMethod.Post, uri, json); string r = response.Content.ReadAsStringAsync().Result; response.EnsureSuccessStatusCode(); } SendRequest (thank you Azure search samples): public static HttpResponseMessage SendRequest(HttpClient client, HttpMethod method, Uri uri, string json = null) { UriBuilder builder = new UriBuilder(uri); //string separator = string.IsNullOrWhiteSpace(builder.Query) ? string.Empty : "&"; //builder.Query = builder.Query.TrimStart('?') + separator + ApiVersionString; var request = new HttpRequestMessage(method, builder.Uri); if (json != null) { request.Content = new StringContent(json, Encoding.UTF8, "application/json"); } return client.SendAsync(request).Result; } Controller Action fragment (o is an empty object here): [HttpPost] public ActionResult CreateTestListing(CreateListingObject o) { Listing li = o.listing; I have confirmed that if I post a simple object using the same code, everything works as expected. Instead of sending a CreateListingObject in PostListing, I send this instead: var test = new { data = "hi mom" }; And change my action to, then the argument gets bound and I get valid data: [HttpPost] public ActionResult CreateTestListing(string data) { I have also checked the serialization of my CreateListingObject in the WebJob, and it is fully populated as I expect. This leads me to suspect that I am falling afoul of the default ModelBinder.
knockout dates being reset on post to mvc controller
I have a knockout/mvc3 application. I am passing the date back to a controller. controller public ActionResult PackageUpdate(Package updatePackage){ \\do some stuff but dates are set to zero? } view model and save method var Package = function (data) { self = this; self = ko.mapping.fromJS(data); self.save = function(){ $.ajax({ url: '/MediaSchedule/PackageUpdate', data:ko.toJSON({ updatePackage: self }) }).success(function (results) { console.log(results); }).error(function (er) { console.error('Ah damn you broke it.') console.log(er); }); } return self; } Json Being passed. {"updatePackage":{"Id":"82e3bc7e-27b8-49c2-b1fa-1ee2ebffbe66","Name":"28a38","SecondaryName":"è€å我è¦é’±","IsLocked":true},"DateCreated":"/Date(1357650000000+1100)/","DateStart":"/Date(1365080400000+1100)/","DateEnd":"/Date(1365516000000+1000)/"} ID, name and other properties are coming through but the date is being reset to {1/1/0001 12:00:00 AM}. My assumption is because it is not being deserialised it is setting a min date. Question: How do I correctly deserialise my date.
I think the problem lies in how you obtaining those dates to begin with. You show an example using MS date format such as /Date(1357650000000+1100)/ which is not standardized and is slowly being deprecated in favor of ISO8601, which looks like 2013-01-08T13:00:00.000+11:00. Indeed, when you JSON.stringify a javascript Date object, it uses the ISO8601 format. This also happens with ko.mapping.toJSON. There are several solutions to this problem, both client side and server side. This post describes the problem in detail and has some great answers that may help you. IMHO, the best solution is have your MVC controller emit and consume ISO8601 rather than the old Microsoft date format. The easiest way to do that is by using the Json.Net library which now has ISO8601 as the default so you don't even need to customize it. On the client side, you might also want to look at Moment.js - which makes it easy to parse and format ISO dates.
I think it just the data type you push to you updatePackage object. Following is my code and run well, I use read the Date from jQuery Datepicker and use the format as 'dd MM, yy' (01 January 2013) var iEndInsuredDate = $('#dpkEndInsuredDate').val(); var iEndPolicyDate = $('#dpkEndPolicyDate').val(); $.ajax({ url: '#Url.Action("DeleteClientMember", "ClientMember")', type: "POST", dataType: "json", data: { clientMemberID: id, endInsuredDate: iEndInsuredDate, endPolicyDate: iEndPolicyDate }, success: function (result) { ShowWaiting("Reloading..."); Search(1); } }); and my ActionResult public ActionResult DeleteClientMember(int clientMemberID, DateTime? endInsuredDate, DateTime? endPolicyDate) { ClientMember model = clientMemberService.GetById(clientMemberID); //model.EndPolicyDate = endPolicyDate; model.EndInsuredDate = endInsuredDate; foreach (ClientMemberProduct item in model.ProductList) { item.EndDate = endInsuredDate; } model.IsActive = false; model.ActionStatus = ClientMemberActionStatus.PendingDelete.ToString(); clientMemberService.CalculateInsFee(model); clientMemberService.Update(model); return null; } Hope this help Regard
Thanks to Matt Johnson I was able to change the way the dates were being sent to the browser. It was a relativly easy fix taken from Perishable Dave answer to a similar problem with ASP.NET MVC JsonResult Date Format My JsonNetResult class now look like public class JsonNetResult : ActionResult { private const string _dateFormat = "yyyy-MM-dd hh:mm:ss"; public Encoding ContentEncoding { get; set; } public string ContentType { get; set; } public object Data { get; set; } public JsonSerializerSettings SerializerSettings { get; set; } public Formatting Formatting { get; set; } public JsonNetResult() { SerializerSettings = new JsonSerializerSettings(); Formatting = Formatting.Indented; SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; } public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); HttpResponseBase response = context.HttpContext.Response; response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; if (ContentEncoding != null) response.ContentEncoding = ContentEncoding; if (Data != null) { var isoConvert = new IsoDateTimeConverter(); isoConvert.DateTimeFormat = _dateFormat; JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting }; JsonSerializer serializer = JsonSerializer.Create(SerializerSettings); serializer.Converters.Add(isoConvert); serializer.Serialize(writer, Data); writer.Flush(); } } } I have added the iso date converter to the serizer in the controller you invoke it by: public JsonNetResult YourAction(){ //your logic here return JsonNetResult(/*your object here*/); } When I wrote this I did not know about Web API. It is worth a look as it will do a lot of the heavy lifting when it comes to serialization of your objects. Checkout Getting Started with ASP.NET Web API 2
JSON collection of objects isn't binding correctly
I have a website where I am uploading an Excel document to the server through AJAX (Telerik control). When the document is uploaded it will return a JSON object collection of currency exchange rates. I then save that collection using the jQuery $.data() method. Then I call the grid to bind and pass my JSON object to my controller to bind the data to the grid. Everything seems to be working up to the point where the Action on the controller binds to the collection. What am I missing, notice how the response from SelectImportedCurrencyRates returns '1/1/1' for dates, country code is 'null' and the exchange rate is '0'. When I Debug and check the bound object ICollection<CurrencyExchangeRate> currencyRatesImported, its bound with those values as '0', '1/1/1' and null Controller [HttpPost] public ActionResult UploadRates(HttpPostedFileBase importRateDocument) { var rates = CurrencyExchangeRateRepository.ReadExcelRates(importRateDocument.InputStream, importRateDocument.FileName); return Json(rates); } [HttpPost] [GridAction] public ActionResult SelectImportedCurrencyRates([Bind(Prefix = "CurrencyRatesImported")] ICollection<CurrencyExchangeRate> currencyRatesImported) { return View(currencyRatesImported != null ? new GridModel(currencyRatesImported) : new GridModel(new List<CurrencyExchangeRate>())); } Javascript <script type="text/javascript"> function onUploadRatesSuccess(e) { $('body').data('CurrencyRatesImported', e.response); var grid = $('#CurrencyExchangeRates').data('tGrid'); grid.ajaxRequest(); } function onCurrencyRatesImportedDataBinding(args) { var currencyRatesImported = $('body').data('CurrencyRatesImported'); console.log(currencyRatesImported); args.data = $.extend(args.data, { CurrencyRatesImported: currencyRatesImported }); args.data = args.data; } e.Response passed to onUploadRatesSuccess (result from ActionResult UploadRates() [{"CountryCode":"CAD","Date":"\/Date(1325656800000)\/","ExchangeRate":1.0145},{"CountryCode":"CAD","Date":"\/Date(1325743200000)\/","ExchangeRate":1.0212},{"CountryCode":"CAD","Date":"\/Date(1325829600000)\/","ExchangeRate":1.0241},{"CountryCode":"CAD","Date":"\/Date(1326088800000)\/","ExchangeRate":1.0265},{"CountryCode":"CAD","Date":"\/Date(1326175200000)\/","ExchangeRate":1.0187},{"CountryCode":"CAD","Date":"\/Date(1326261600000)\/","ExchangeRate":1.0209},{"CountryCode":"CAD","Date":"\/Date(1326348000000)\/","ExchangeRate":1.0207},{"CountryCode":"CAD","Date":"\/Date(1326434400000)\/","ExchangeRate":1.0245},{"CountryCode":"CAD","Date":"\/Date(1326693600000)\/","ExchangeRate":1.0182},{"CountryCode":"CAD","Date":"\/Date(1326780000000)\/","ExchangeRate":1.0143},{"CountryCode":"CAD","Date":"\/Date(1326866400000)\/","ExchangeRate":1.0139},{"CountryCode":"CAD","Date":"\/Date(1326952800000)\/","ExchangeRate":1.0091},{"CountryCode":"CAD","Date":"\/Date(1327039200000)\/","ExchangeRate":1.014},{"CountryCode":"CAD","Date":"\/Date(1327298400000)\/","ExchangeRate":1.0066},{"CountryCode":"CAD","Date":"\/Date(1327384800000)\/","ExchangeRate":1.0108},{"CountryCode":"CAD","Date":"\/Date(1327471200000)\/","ExchangeRate":1.0121},{"CountryCode":"CAD","Date":"\/Date(1327557600000)\/","ExchangeRate":0.9997},{"CountryCode":"CAD","Date":"\/Date(1327644000000)\/","ExchangeRate":1.0015},{"CountryCode":"CAD","Date":"\/Date(1327903200000)\/","ExchangeRate":1.0046},{"CountryCode":"CAD","Date":"\/Date(1328162400000)\/","ExchangeRate":0.9978},{"CountryCode":"CAD","Date":"\/Date(1328248800000)\/","ExchangeRate":0.9986},{"CountryCode":"CAD","Date":"\/Date(1328508000000)\/","ExchangeRate":0.9957},{"CountryCode":"CAD","Date":"\/Date(1328594400000)\/","ExchangeRate":0.9949},{"CountryCode":"CAD","Date":"\/Date(1328680800000)\/","ExchangeRate":0.9953},{"CountryCode":"CAD","Date":"\/Date(1328767200000)\/","ExchangeRate":0.994},{"CountryCode":"CAD","Date":"\/Date(1328853600000)\/","ExchangeRate":0.9956},{"CountryCode":"CAD","Date":"\/Date(1329112800000)\/","ExchangeRate":0.9998},{"CountryCode":"CAD","Date":"\/Date(1329199200000)\/","ExchangeRate":1.0007},{"CountryCode":"CAD","Date":"\/Date(1329285600000)\/","ExchangeRate":0.9976},{"CountryCode":"CAD","Date":"\/Date(1329372000000)\/","ExchangeRate":1.0003},{"CountryCode":"CAD","Date":"\/Date(1329458400000)\/","ExchangeRate":0.9969},{"CountryCode":"CAD","Date":"\/Date(1329717600000)\/","ExchangeRate":0.9924},{"CountryCode":"CAD","Date":"\/Date(1329804000000)\/","ExchangeRate":0.9957},{"CountryCode":"CAD","Date":"\/Date(1329890400000)\/","ExchangeRate":0.9996},{"CountryCode":"CAD","Date":"\/Date(1329976800000)\/","ExchangeRate":0.9973},{"CountryCode":"CAD","Date":"\/Date(1330063200000)\/","ExchangeRate":0.9987},{"CountryCode":"CAD","Date":"\/Date(1330322400000)\/","ExchangeRate":1.0002},{"CountryCode":"CAD","Date":"\/Date(1330408800000)\/","ExchangeRate":0.9958},{"CountryCode":"CAD","Date":"\/Date(1330581600000)\/","ExchangeRate":0.9958},{"CountryCode":"CAD","Date":"\/Date(1330668000000)\/","ExchangeRate":0.9878},{"CountryCode":"CAD","Date":"\/Date(1330927200000)\/","ExchangeRate":0.9937},{"CountryCode":"CAD","Date":"\/Date(1331013600000)\/","ExchangeRate":1.0017},{"CountryCode":"CAD","Date":"\/Date(1331100000000)\/","ExchangeRate":1.0023},{"CountryCode":"CAD","Date":"\/Date(1331186400000)\/","ExchangeRate":0.9934},{"CountryCode":"CAD","Date":"\/Date(1331272800000)\/","ExchangeRate":0.9889},{"CountryCode":"CAD","Date":"\/Date(1331528400000)\/","ExchangeRate":0.9935},{"CountryCode":"CAD","Date":"\/Date(1331614800000)\/","ExchangeRate":0.9906},{"CountryCode":"CAD","Date":"\/Date(1331701200000)\/","ExchangeRate":0.9915},{"CountryCode":"CAD","Date":"\/Date(1331787600000)\/","ExchangeRate":0.9929},{"CountryCode":"CAD","Date":"\/Date(1331874000000)\/","ExchangeRate":0.9913},{"CountryCode":"CAD","Date":"\/Date(1332133200000)\/","ExchangeRate":0.988},{"CountryCode":"CAD","Date":"\/Date(1332219600000)\/","ExchangeRate":0.9934},{"CountryCode":"CAD","Date":"\/Date(1332306000000)\/","ExchangeRate":0.9931},{"CountryCode":"CAD","Date":"\/Date(1332392400000)\/","ExchangeRate":1.0003},{"CountryCode":"CAD","Date":"\/Date(1332478800000)\/","ExchangeRate":0.9984},{"CountryCode":"CAD","Date":"\/Date(1332738000000)\/","ExchangeRate":0.9915},{"CountryCode":"CAD","Date":"\/Date(1332824400000)\/","ExchangeRate":0.9925},{"CountryCode":"CAD","Date":"\/Date(1332910800000)\/","ExchangeRate":0.9965},{"CountryCode":"CAD","Date":"\/Date(1332997200000)\/","ExchangeRate":0.9992},{"CountryCode":"CAD","Date":"\/Date(1333429200000)\/","ExchangeRate":0.9896},{"CountryCode":"CAD","Date":"\/Date(1333515600000)\/","ExchangeRate":0.9971},{"CountryCode":"CAD","Date":"\/Date(1333602000000)\/","ExchangeRate":0.99385},{"CountryCode":"CAD","Date":"\/Date(1333688400000)\/","ExchangeRate":0.99565},{"CountryCode":"CAD","Date":"\/Date(1333947600000)\/","ExchangeRate":0.99761},{"CountryCode":"CAD","Date":"\/Date(1334034000000)\/","ExchangeRate":1.0027},{"CountryCode":"CAD","Date":"\/Date(1334120400000)\/","ExchangeRate":1.002},{"CountryCode":"CAD","Date":"\/Date(1334206800000)\/","ExchangeRate":0.9966},{"CountryCode":"CAD","Date":"\/Date(1334293200000)\/","ExchangeRate":0.9975},{"CountryCode":"CAD","Date":"\/Date(1334552400000)\/","ExchangeRate":1.0016},{"CountryCode":"CAD","Date":"\/Date(1334638800000)\/","ExchangeRate":0.9876},{"CountryCode":"CAD","Date":"\/Date(1334725200000)\/","ExchangeRate":0.9958},{"CountryCode":"CAD","Date":"\/Date(1334811600000)\/","ExchangeRate":0.99},{"CountryCode":"CAD","Date":"\/Date(1334898000000)\/","ExchangeRate":0.9909},{"CountryCode":"CAD","Date":"\/Date(1335157200000)\/","ExchangeRate":0.995},{"CountryCode":"CAD","Date":"\/Date(1335243600000)\/","ExchangeRate":0.9885},{"CountryCode":"CAD","Date":"\/Date(1335330000000)\/","ExchangeRate":0.9853},{"CountryCode":"CAD","Date":"\/Date(1335416400000)\/","ExchangeRate":0.9841},{"CountryCode":"CAD","Date":"\/Date(1335502800000)\/","ExchangeRate":0.981},{"CountryCode":"CAD","Date":"\/Date(1335848400000)\/","ExchangeRate":0.981},{"CountryCode":"CAD","Date":"\/Date(1335934800000)\/","ExchangeRate":0.9891},{"CountryCode":"CAD","Date":"\/Date(1336021200000)\/","ExchangeRate":0.9867},{"CountryCode":"CAD","Date":"\/Date(1336107600000)\/","ExchangeRate":0.9863},{"CountryCode":"CAD","Date":"\/Date(1336366800000)\/","ExchangeRate":0.99636},{"CountryCode":"CAD","Date":"\/Date(1336453200000)\/","ExchangeRate":0.996},{"CountryCode":"CAD","Date":"\/Date(1336539600000)\/","ExchangeRate":1.0044},{"CountryCode":"CAD","Date":"\/Date(1336626000000)\/","ExchangeRate":1.0022},{"CountryCode":"CAD","Date":"\/Date(1336712400000)\/","ExchangeRate":0.9968},{"CountryCode":"CAD","Date":"\/Date(1336971600000)\/","ExchangeRate":1.0024},{"CountryCode":"CAD","Date":"\/Date(1337058000000)\/","ExchangeRate":1.0038},{"CountryCode":"CAD","Date":"\/Date(1337144400000)\/","ExchangeRate":1.0088},{"CountryCode":"CAD","Date":"\/Date(1337230800000)\/","ExchangeRate":1.0163},{"CountryCode":"CAD","Date":"\/Date(1337317200000)\/","ExchangeRate":1.0179},{"CountryCode":"CAD","Date":"\/Date(1337576400000)\/","ExchangeRate":1.0218},{"CountryCode":"CAD","Date":"\/Date(1337662800000)\/","ExchangeRate":1.0172},{"CountryCode":"CAD","Date":"\/Date(1337749200000)\/","ExchangeRate":1.0283},{"CountryCode":"CAD","Date":"\/Date(1337835600000)\/","ExchangeRate":1.0262},{"CountryCode":"CAD","Date":"\/Date(1337922000000)\/","ExchangeRate":1.0289},{"CountryCode":"CAD","Date":"\/Date(1338181200000)\/","ExchangeRate":1.0253},{"CountryCode":"CAD","Date":"\/Date(1338267600000)\/","ExchangeRate":1.0222},{"CountryCode":"CAD","Date":"\/Date(1338354000000)\/","ExchangeRate":1.0297},{"CountryCode":"CAD","Date":"\/Date(1338526800000)\/","ExchangeRate":1.0297},{"CountryCode":"CAD","Date":"\/Date(1338786000000)\/","ExchangeRate":1.04141},{"CountryCode":"CAD","Date":"\/Date(1338872400000)\/","ExchangeRate":1.04154},{"CountryCode":"CAD","Date":"\/Date(1338958800000)\/","ExchangeRate":1.0333},{"CountryCode":"CAD","Date":"\/Date(1339045200000)\/","ExchangeRate":1.0242},{"CountryCode":"CAD","Date":"\/Date(1339131600000)\/","ExchangeRate":1.0314},{"CountryCode":"CAD","Date":"\/Date(1339390800000)\/","ExchangeRate":1.0289},{"CountryCode":"CAD","Date":"\/Date(1339477200000)\/","ExchangeRate":1.0293},{"CountryCode":"CAD","Date":"\/Date(1339563600000)\/","ExchangeRate":1.025},{"CountryCode":"CAD","Date":"\/Date(1339650000000)\/","ExchangeRate":1.0252},{"CountryCode":"CAD","Date":"\/Date(1339736400000)\/","ExchangeRate":1.0236},{"CountryCode":"CAD","Date":"\/Date(1339995600000)\/","ExchangeRate":1.0254},{"CountryCode":"CAD","Date":"\/Date(1340082000000)\/","ExchangeRate":1.018},{"CountryCode":"CAD","Date":"\/Date(1340168400000)\/","ExchangeRate":1.0202},{"CountryCode":"CAD","Date":"\/Date(1340254800000)\/","ExchangeRate":1.0231},{"CountryCode":"CAD","Date":"\/Date(1340341200000)\/","ExchangeRate":1.0272},{"CountryCode":"CAD","Date":"\/Date(1340600400000)\/","ExchangeRate":1.0304},{"CountryCode":"CAD","Date":"\/Date(1340773200000)\/","ExchangeRate":1.0283},{"CountryCode":"CAD","Date":"\/Date(1340859600000)\/","ExchangeRate":1.0339},{"CountryCode":"CAD","Date":"\/Date(1341205200000)\/","ExchangeRate":1.0261},{"CountryCode":"CAD","Date":"\/Date(1341291600000)\/","ExchangeRate":1.0132},{"CountryCode":"CAD","Date":"\/Date(1341378000000)\/","ExchangeRate":1.0126},{"CountryCode":"CAD","Date":"\/Date(1341464400000)\/","ExchangeRate":1.0139},{"CountryCode":"CAD","Date":"\/Date(1341550800000)\/","ExchangeRate":1.0188},{"CountryCode":"CAD","Date":"\/Date(1341810000000)\/","ExchangeRate":1.0199},{"CountryCode":"CAD","Date":"\/Date(1341896400000)\/","ExchangeRate":1.0209},{"CountryCode":"CAD","Date":"\/Date(1341982800000)\/","ExchangeRate":1.0206},{"CountryCode":"CAD","Date":"\/Date(1342069200000)\/","ExchangeRate":1.0248},{"CountryCode":"CAD","Date":"\/Date(1342155600000)\/","ExchangeRate":1.014},{"CountryCode":"CAD","Date":"\/Date(1342414800000)\/","ExchangeRate":1.0159},{"CountryCode":"CAD","Date":"\/Date(1342501200000)\/","ExchangeRate":1.0166},{"CountryCode":"CAD","Date":"\/Date(1342587600000)\/","ExchangeRate":1.0109},{"CountryCode":"CAD","Date":"\/Date(1342674000000)\/","ExchangeRate":1.0073},{"CountryCode":"CAD","Date":"\/Date(1342760400000)\/","ExchangeRate":1.0102},{"CountryCode":"CAD","Date":"\/Date(1343019600000)\/","ExchangeRate":1.0176},{"CountryCode":"CAD","Date":"\/Date(1343106000000)\/","ExchangeRate":1.0198},{"CountryCode":"CAD","Date":"\/Date(1343192400000)\/","ExchangeRate":1.0195},{"CountryCode":"CAD","Date":"\/Date(1343278800000)\/","ExchangeRate":1.0097},{"CountryCode":"CAD","Date":"\/Date(1343365200000)\/","ExchangeRate":1.0058},{"CountryCode":"CAD","Date":"\/Date(1343624400000)\/","ExchangeRate":1.0027},{"CountryCode":"CAD","Date":"\/Date(1343797200000)\/","ExchangeRate":1.0027},{"CountryCode":"CAD","Date":"\/Date(1343883600000)\/","ExchangeRate":1.0041},{"CountryCode":"CAD","Date":"\/Date(1343970000000)\/","ExchangeRate":0.9997},{"CountryCode":"CAD","Date":"\/Date(1344229200000)\/","ExchangeRate":0.9995},{"CountryCode":"CAD","Date":"\/Date(1344315600000)\/","ExchangeRate":0.9967},{"CountryCode":"CAD","Date":"\/Date(1344402000000)\/","ExchangeRate":0.995},{"CountryCode":"CAD","Date":"\/Date(1344488400000)\/","ExchangeRate":0.9917},{"CountryCode":"CAD","Date":"\/Date(1344574800000)\/","ExchangeRate":0.991},{"CountryCode":"CAD","Date":"\/Date(1344834000000)\/","ExchangeRate":0.9932},{"CountryCode":"CAD","Date":"\/Date(1344920400000)\/","ExchangeRate":0.9919},{"CountryCode":"CAD","Date":"\/Date(1345006800000)\/","ExchangeRate":0.9897},{"CountryCode":"CAD","Date":"\/Date(1345093200000)\/","ExchangeRate":0.9883},{"CountryCode":"CAD","Date":"\/Date(1345179600000)\/","ExchangeRate":0.9882},{"CountryCode":"CAD","Date":"\/Date(1345438800000)\/","ExchangeRate":0.9894},{"CountryCode":"CAD","Date":"\/Date(1345525200000)\/","ExchangeRate":0.9855},{"CountryCode":"CAD","Date":"\/Date(1345611600000)\/","ExchangeRate":0.993},{"CountryCode":"CAD","Date":"\/Date(1345698000000)\/","ExchangeRate":0.9916},{"CountryCode":"CAD","Date":"\/Date(1345784400000)\/","ExchangeRate":0.99121},{"CountryCode":"CAD","Date":"\/Date(1346043600000)\/","ExchangeRate":0.99234},{"CountryCode":"CAD","Date":"\/Date(1346130000000)\/","ExchangeRate":0.9855},{"CountryCode":"CAD","Date":"\/Date(1346216400000)\/","ExchangeRate":0.9885},{"CountryCode":"CAD","Date":"\/Date(1346302800000)\/","ExchangeRate":0.9923},{"CountryCode":"CAD","Date":"\/Date(1346648400000)\/","ExchangeRate":0.9923},{"CountryCode":"CAD","Date":"\/Date(1346734800000)\/","ExchangeRate":0.9865},{"CountryCode":"CAD","Date":"\/Date(1346821200000)\/","ExchangeRate":0.9917},{"CountryCode":"CAD","Date":"\/Date(1346907600000)\/","ExchangeRate":0.9819},{"CountryCode":"CAD","Date":"\/Date(1346994000000)\/","ExchangeRate":0.9771},{"CountryCode":"CAD","Date":"\/Date(1347253200000)\/","ExchangeRate":0.9757},{"CountryCode":"CAD","Date":"\/Date(1347339600000)\/","ExchangeRate":0.9727},{"CountryCode":"CAD","Date":"\/Date(1347426000000)\/","ExchangeRate":0.9751},{"CountryCode":"CAD","Date":"\/Date(1347512400000)\/","ExchangeRate":0.9751},{"CountryCode":"CAD","Date":"\/Date(1347598800000)\/","ExchangeRate":0.9687},{"CountryCode":"CAD","Date":"\/Date(1347858000000)\/","ExchangeRate":0.9719},{"CountryCode":"CAD","Date":"\/Date(1347944400000)\/","ExchangeRate":0.9743},{"CountryCode":"CAD","Date":"\/Date(1348030800000)\/","ExchangeRate":0.9761},{"CountryCode":"CAD","Date":"\/Date(1348117200000)\/","ExchangeRate":0.9777},{"CountryCode":"CAD","Date":"\/Date(1348203600000)\/","ExchangeRate":0.9763},{"CountryCode":"CAD","Date":"\/Date(1348462800000)\/","ExchangeRate":0.9795},{"CountryCode":"CAD","Date":"\/Date(1348549200000)\/","ExchangeRate":0.9769},{"CountryCode":"CAD","Date":"\/Date(1348635600000)\/","ExchangeRate":0.9855},{"CountryCode":"CAD","Date":"\/Date(1348722000000)\/","ExchangeRate":0.984},{"CountryCode":"CAD","Date":"\/Date(1349067600000)\/","ExchangeRate":0},{"CountryCode":"CAD","Date":"\/Date(1349154000000)\/","ExchangeRate":0.9832},{"CountryCode":"CAD","Date":"\/Date(1349240400000)\/","ExchangeRate":0.988},{"CountryCode":"CAD","Date":"\/Date(1349326800000)\/","ExchangeRate":0.9816},{"CountryCode":"CAD","Date":"\/Date(1349413200000)\/","ExchangeRate":0.9756},{"CountryCode":"CAD","Date":"\/Date(1349672400000)\/","ExchangeRate":0.9761},{"CountryCode":"CAD","Date":"\/Date(1349758800000)\/","ExchangeRate":0.9783},{"CountryCode":"CAD","Date":"\/Date(1349845200000)\/","ExchangeRate":0.9787},{"CountryCode":"CAD","Date":"\/Date(1349931600000)\/","ExchangeRate":0.977},{"CountryCode":"CAD","Date":"\/Date(1350018000000)\/","ExchangeRate":0.9801},{"CountryCode":"CAD","Date":"\/Date(1350277200000)\/","ExchangeRate":0.9805},{"CountryCode":"CAD","Date":"\/Date(1350363600000)\/","ExchangeRate":0.9861},{"CountryCode":"CAD","Date":"\/Date(1350450000000)\/","ExchangeRate":0.9818},{"CountryCode":"CAD","Date":"\/Date(1350536400000)\/","ExchangeRate":0.9823},{"CountryCode":"CAD","Date":"\/Date(1350622800000)\/","ExchangeRate":0.991},{"CountryCode":"CAD","Date":"\/Date(1350882000000)\/","ExchangeRate":0.9933},{"CountryCode":"CAD","Date":"\/Date(1350968400000)\/","ExchangeRate":0.9926},{"CountryCode":"CAD","Date":"\/Date(1351054800000)\/","ExchangeRate":0.9916},{"CountryCode":"CAD","Date":"\/Date(1351141200000)\/","ExchangeRate":0.9938},{"CountryCode":"CAD","Date":"\/Date(1351227600000)\/","ExchangeRate":0.9976},{"CountryCode":"CAD","Date":"\/Date(1351486800000)\/","ExchangeRate":1.0004},{"CountryCode":"CAD","Date":"\/Date(1351573200000)\/","ExchangeRate":0.9993},{"CountryCode":"CAD","Date":"\/Date(1351746000000)\/","ExchangeRate":0.9993},{"CountryCode":"CAD","Date":"\/Date(1351832400000)\/","ExchangeRate":0.99925},{"CountryCode":"CAD","Date":"\/Date(1352095200000)\/","ExchangeRate":0.9966},{"CountryCode":"CAD","Date":"\/Date(1352181600000)\/","ExchangeRate":0.9948},{"CountryCode":"CAD","Date":"\/Date(1352268000000)\/","ExchangeRate":0.9959},{"CountryCode":"CAD","Date":"\/Date(1352354400000)\/","ExchangeRate":0.9979},{"CountryCode":"CAD","Date":"\/Date(1352440800000)\/","ExchangeRate":1.0017},{"CountryCode":"CAD","Date":"\/Date(1352700000000)\/","ExchangeRate":1.0004},{"CountryCode":"CAD","Date":"\/Date(1352786400000)\/","ExchangeRate":1.0013},{"CountryCode":"CAD","Date":"\/Date(1352872800000)\/","ExchangeRate":1.0032},{"CountryCode":"CAD","Date":"\/Date(1352959200000)\/","ExchangeRate":1.0027},{"CountryCode":"CAD","Date":"\/Date(1353045600000)\/","ExchangeRate":1.0034},{"CountryCode":"CAD","Date":"\/Date(1353304800000)\/","ExchangeRate":0.9956},{"CountryCode":"CAD","Date":"\/Date(1353391200000)\/","ExchangeRate":0.9979},{"CountryCode":"CAD","Date":"\/Date(1353477600000)\/","ExchangeRate":0.9977},{"CountryCode":"CAD","Date":"\/Date(1353564000000)\/","ExchangeRate":0.9981},{"CountryCode":"CAD","Date":"\/Date(1353650400000)\/","ExchangeRate":0.9928},{"CountryCode":"CAD","Date":"\/Date(1353909600000)\/","ExchangeRate":0.9951},{"CountryCode":"CAD","Date":"\/Date(1353996000000)\/","ExchangeRate":0.9943},{"CountryCode":"CAD","Date":"\/Date(1354082400000)\/","ExchangeRate":0.9936},{"CountryCode":"CAD","Date":"\/Date(1354168800000)\/","ExchangeRate":0.9917},{"CountryCode":"CAD","Date":"\/Date(1354514400000)\/","ExchangeRate":0.9917},{"CountryCode":"CAD","Date":"\/Date(1354600800000)\/","ExchangeRate":0.9927},{"CountryCode":"CAD","Date":"\/Date(1354687200000)\/","ExchangeRate":0.9929},{"CountryCode":"CAD","Date":"\/Date(1354773600000)\/","ExchangeRate":0.9895},{"CountryCode":"CAD","Date":"\/Date(1354860000000)\/","ExchangeRate":0.9887},{"CountryCode":"CAD","Date":"\/Date(1355119200000)\/","ExchangeRate":0.9872},{"CountryCode":"CAD","Date":"\/Date(1355205600000)\/","ExchangeRate":0.9868},{"CountryCode":"CAD","Date":"\/Date(1355292000000)\/","ExchangeRate":0.9852},{"CountryCode":"CAD","Date":"\/Date(1355378400000)\/","ExchangeRate":0.9841},{"CountryCode":"CAD","Date":"\/Date(1355464800000)\/","ExchangeRate":0.9866},{"CountryCode":"CAD","Date":"\/Date(1355724000000)\/","ExchangeRate":0.9844},{"CountryCode":"CAD","Date":"\/Date(1355810400000)\/","ExchangeRate":0.9846},{"CountryCode":"CAD","Date":"\/Date(1355896800000)\/","ExchangeRate":0.9869},{"CountryCode":"CAD","Date":"\/Date(1355983200000)\/","ExchangeRate":0.9887},{"CountryCode":"CAD","Date":"\/Date(1356069600000)\/","ExchangeRate":0.9947},{"CountryCode":"CAD","Date":"\/Date(1356328800000)\/","ExchangeRate":0.99358},{"CountryCode":"CAD","Date":"\/Date(1356415200000)\/","ExchangeRate":0.99304},{"CountryCode":"CAD","Date":"\/Date(1356501600000)\/","ExchangeRate":0.99125},{"CountryCode":"CAD","Date":"\/Date(1356588000000)\/","ExchangeRate":0.9936},{"CountryCode":"CAD","Date":"\/Date(1356674400000)\/","ExchangeRate":0.9956},{"CountryCode":"CAD","Date":"\/Date(1357106400000)\/","ExchangeRate":0.9837},{"CountryCode":"CAD","Date":"\/Date(1357192800000)\/","ExchangeRate":0.9851},{"CountryCode":"CAD","Date":"\/Date(1357279200000)\/","ExchangeRate":0.9857},{"CountryCode":"CAD","Date":"\/Date(1357538400000)\/","ExchangeRate":0.9859},{"CountryCode":"CAD","Date":"\/Date(1357624800000)\/","ExchangeRate":0.9871},{"CountryCode":"CAD","Date":"\/Date(1357711200000)\/","ExchangeRate":0.9874},{"CountryCode":"CAD","Date":"\/Date(1357797600000)\/","ExchangeRate":0.9866},{"CountryCode":"CAD","Date":"\/Date(1357884000000)\/","ExchangeRate":0.9837},{"CountryCode":"CAD","Date":"\/Date(1358143200000)\/","ExchangeRate":0.9857},{"CountryCode":"CAD","Date":"\/Date(1358229600000)\/","ExchangeRate":0.9837},{"CountryCode":"CAD","Date":"\/Date(1358316000000)\/","ExchangeRate":0.9858},{"CountryCode":"CAD","Date":"\/Date(1358402400000)\/","ExchangeRate":0.9854},{"CountryCode":"CAD","Date":"\/Date(1358488800000)\/","ExchangeRate":0.9938},{"CountryCode":"CAD","Date":"\/Date(1358748000000)\/","ExchangeRate":0.9939},{"CountryCode":"CAD","Date":"\/Date(1358834400000)\/","ExchangeRate":0.9935},{"CountryCode":"CAD","Date":"\/Date(1358920800000)\/","ExchangeRate":0.9986},{"CountryCode":"CAD","Date":"\/Date(1359007200000)\/","ExchangeRate":1.0032},{"CountryCode":"CAD","Date":"\/Date(1359093600000)\/","ExchangeRate":1.0087},{"CountryCode":"CAD","Date":"\/Date(1359352800000)\/","ExchangeRate":1.0092},{"CountryCode":"CAD","Date":"\/Date(1359439200000)\/","ExchangeRate":1.0042},{"CountryCode":"CAD","Date":"\/Date(1359525600000)\/","ExchangeRate":1.0029}] Binding call to SelectImportedCurrencyRates Response from SelectImportedCurrencyRates CurrencyExchangeRate class [Serializable()] public class CurrencyExchangeRate { public string CountryCode { get; set; } public System.DateTime Date { get; set; } public double ExchangeRate { get; set; } }
The problem was in the serialization of the collection I was sending up. It was passing an array up, instead of a serialized JSON object. Here is what the serialization method should look like. function serialize(prefix, data) { var result = {}, dateRegex = /^\/Date\((.*?)\)\/$/; for (var i = 0; i < data.length; i++) { for (var field in data[i]) { var value = data[i][field]; if (typeof value === "string") { var date = dateRegex.exec(value); if (date) { value = $.telerik.formatString("{0:d}", new Date(parseInt(date[1]))); } } result[prefix + "[" + i + "]." + field] = value; } } return result; } So then when I call onCurrencyRatesImportedDataBinding it will serialize my object of currency exchange rates and pass them up properly. function onCurrencyRatesImportedDataBinding(args) { var currencyRatesImported = $('body').data('CurrencyRatesImported'); console.log(currencyRatesImported); args.data = $.extend(args.data, serialize("currencyExchangeRates", currencyRatesImported)); args.data = args.data; }
I believe the issue is the format of the dates being sent across the wire. Unfortunately the DefaultModelBinder in MVC doesn't know how to bind these dates. You'll need to extend the DefaultModelBinder. See e.g. http://taintedsanctity.tumblr.com/post/6425063095/asp-mvc-model-binding-json-dates-serialized-in https://stackoverflow.com/a/7849322/206297
Return PDF to browser using JSON and MVC?
I have a link as follows. #Html.ActionLink("Create Report", "Screenreport", "Reports", null, new { #class = "subNavA AddBorderTop", id = "screenReport", title = "Create Report" }) Once the link is clicked, I have a the following jQuery code which creates a JSON object and post the information. $().ready(function () { // Create Report fron the screen data $("#screenReport").live("click", function (event) { GenerateScreenReport(this, event); }); }) /* end document.ready() */ function GenerateScreenReport(clikedtag, event) { var table = $(".EvrakTable").html(); var screendata = tableParser(table); var Screentable = { Screenlist: screendata }; var myurl = $(clikedtag).attr("href"); var title = $(clikedtag).attr("title"); $.ajax({ url: myurl, type: 'POST', data: JSON.stringify(Screentable), dataType: 'json', contentType: 'application/json', success: function () { alert("Got it"); } }); }; To Handle JSON I have the following two classes. Realize two classes in the same namespace namespace MyProject.ViewModels { public class Screenrecord { public string Fname{ get; set; } public string LName { get; set; } public string Age { get; set; } public string DOB { get; set; } } public class Screentable { public List<Screenrecord> Screenlist { get; set; } } } ANd in my controller, I have the following code: [HttpPost] public FileStreamResult Screenreport(Screentable screendata) { MemoryStream outputStream = new MemoryStream(); MemoryStream workStream = new MemoryStream(); Document document = new Document(); PdfWriter.GetInstance(document, workStream); document.Open(); document.Add(new Paragraph("Hello World")); document.Add(new Paragraph(DateTime.Now.ToString())); document.Close(); byte[] byteInfo = workStream.ToArray(); outputStream.Write(byteInfo, 0, byteInfo.Length); outputStream.Position = 0; return new FileStreamResult(outputStream, "application/pdf"); } This code is supposed to gerate PDF. if I leave [HttpPost] as it is, it does NOT generate PDF and it goes to /Screenreport page, however I see my JSON is passed to the controller properly. (screendata is populated properly - in controller) But if I comment out [HttpPost], it DOES generate a PDF but screendata (in controller) is null. Can someone please explain whats's going on and help me figure it out. Thanksin advance.
You cannot use AJAX to download files, because javascript doesn't allow you to save the downloaded content. To workaround this you need to take 2 steps. First: make the HTTP Post request, and in the controller action we would store the File content in a Memory stream.Second: on success make another call by setting the window.location to the Download Action method In your Controller create this 2 actions: public ActionResult GenerateFile() { MemoryStream fileStream = new MemoryStream { Position = 0 }; //position = 0 is important var fName = string.Format("File-{0}.xlsx", DateTime.Now.ToString("s")); Session[fName] = fileStream; return Json(new { success = true, fName }, JsonRequestBehavior.AllowGet); } public ActionResult DownloadFile(string fName) { var ms = Session[fName] as MemoryStream; if (ms == null) return new EmptyResult(); Session[fName] = null; return File(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fName); } In your javascript: $('#Donwload-button').click(function () { data = JSON.stringify(YOURDATA); $.ajax({ contentType: 'application/json; charset=utf-8', dataType: 'json', type: 'POST', url: "/YOURCONTROLLER/GenerateFile", data: data, success: function (d) { if (d.success) { window.location = "/YOURCONTROLLER/DownloadFile" + "?fName=" + d.fName; } }, error: function () { alert("Error"); } }); });
I feel obligated to post my answer since I didn't hear from anyone. I ended up creating a form that includes a hidden input, then saved my json object in the hidden input and then submit the form. This time I will get input as an string not a json or xml. var $hidInput = $("#dataToReport"); $hidInput.val(JSON.stringify(Screentable)); $('#frmScreenreport').submit(); Thanks all anyways.