How to pass user input from view to a class in MVC - asp.net-mvc

I imagine this must be pretty basic, but all my search results only show how to pass data the other way ( model to controller, controller to view, and then view back to controller, but nothing from controller back to model(class). I am trying to pass user input for a search parameter into the query string for an API. In the view:
<form method="post" action="~/Models/Search">
<input name="artist" placeholder="Enter artist's name" />
#{Search temp = new Search();
if (Request.Form["artist"] != null) //this method doesn't work; trying to get user response and pass it back to search class;
{ temp.Artist = Request.Form["artist"].ToLower(); } //have to hardcode search parameter at this time
}
<!--<button>Click Me</button>-->
<script>
var weather = Object();
$(document).ready(function () {
$('button').click(function () {
// var artist = $("#artist").val();
$.get("#Url.Action("SearchArtist", "Home")", function (response) {
console.log(response);
artistInfo = ko.mapping.fromJS(response); //populate the artist search object
ko.applyBindings(artistInfo);
});
});
});
The class:
public class Search
{
string artist;
public string Artist { get; set;}
public Object getArtistInfo()
{
string appID = "*************** ";
artist = "metallica";
string url = "http://developer.echonest.com/api/v4/artist/search?api_key="+ appID + "&format=json&name=" + artist + "&results=1&bucket=genre&bucket=songs";
//synchronous client;
var client = new WebClient();
var content = client.DownloadString(url);
var serializer = new JavaScriptSerializer();
var jsonContent = serializer.Deserialize<Object>(content);
return jsonContent;
}
}
The controller:
public ActionResult ArtistInfo()
{
return View();
}
public JsonResult SearchArtist()
{
Search artist = new Search();
return Json(artist.getArtistInfo(), JsonRequestBehavior.AllowGet);
}

Try to add attributes (runat="server" and id="chooseyourid") to html forms results below :
if you want to access to values in your code behinde you just need to write the id of your element like below :
/artiste.value;/

Related

MVC Form with Sitecore File Post through Form returns Null

For the possible duplicate, I already know how to preview my image before uploading it, my issue as detailed below is that when I submit the Form, the image is being received as null.
I am trying to submit a form to an MVC controller that should submit a model, a string, and an Image File,
I made sure that the input has the same name as the parameter within the controller
Following is the Form Initialization code
#using (Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName,
new
{
calendar = System.Convert.ToString(Request.QueryString["calendar"]),
ID = System.Convert.ToString(Request.QueryString["id"])
},
FormMethod.Post, new { enctype = "multipart/form-data" }))
{
//Model Input
<input style="opacity: 0;" name="EventImage" type="file" accept="image/jpeg, image/jpg" id="ImageUpload" onchange="readURL(this);" />
}
And the Controller Header
[HttpPost]
[ValidateInput(false)]
public ActionResult AddEvent(Event model, string calendar, HttpPostedFileBase EventImage)
The EventImage Parameter is being returned null and I can't seem to figure out why.
I thought that the ID might be causing the problem so I changed the name attribute to ImageUpload as well as the Parameter within the controller but to no avail as the value is still null.
Additional Info: when a User uploads an image, I let them preview it in an Image box, could that be causing it?
Thanks,
Update:
Here is the code for the readURL function
function readURL(input) {
if (input.files && input.files[0]) {
var ImageCorrect = false;
var file = input.files[0];
var reader = new FileReader();
reader.onload = function (e) {
// Concatenate our HTML image info
var ext = file.name.match(/\.([^\.]+)$/)[1];
switch (ext) {
case 'jpg':
case 'jpeg':
case 'JPG':
case 'JPEG':
{
if ((Math.round(file.size / 1024)) > 500) {
alert('Image is too Large');
}
else {
var image = new Image();
image.src = e.target.result;
image.onload = function () {
var width = parseInt(image.width);
if (width <= 500) {
$('#previewImage').attr('src', e.target.result);
$('#previewImage').show();
}
else {
alert('Image width exceeds maximum width');
}
}
}
}
break;
default:
alert('Image type not allowed')
}
}
reader.readAsDataURL(input.files[0]);
}
}
If I understood your question right you are trying to submit a file from your form to your controller and you get null in the controller.
I did this before, check the following:
cshtml (you can add your attributes to event image like JS call...etc ):
<div class="form-group">
#Sitecore.Globalization.Translate.Text("EventImage")<br>
#Html.TextBoxFor(m => m.EventImage, new { type = "file" })
</div>
Model:
[Display(Name = "Event Image")]
public HttpPostedFileBase EventImage { get; set; }
Controller Signature:
[HttpPost]
[ValidateInput(false)]
public ActionResult AddEvent(Event model)
Catching the Image field:
if (model.EventImage != null && model.EventImage.ContentLength > 0)
{
var fileName = Path.GetFileName(model.EventImage.FileName);
var tempPath = Server.MapPath("~/Temp/uploads");
var path = Path.Combine(tempPath, fileName);
if (!Directory.Exists(tempPath))
Directory.CreateDirectory(tempPath);
model.EventImage.SaveAs(path);
Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions();
options.FileBased = false;
options.IncludeExtensionInItemName = false;
options.KeepExisting = false;
options.Versioned = false;
options.Destination = "/sitecore/media library/Images/" + ItemUtil.ProposeValidItemName(Path.GetFileName(path));
options.Database = Sitecore.Configuration.Factory.GetDatabase(MasterDatabase);
// Now create the file
Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator();
MediaItem mediaItem = creator.CreateFromFile(path, options);
ImageField _eventImage = (ImageField)_event.Fields[EventImage];
_eventImage.MediaID = mediaItem.ID;
PublishItem(mediaItem);
}

Controller name being lost when trying to post to controller action after file upload

I have a MVC Controller which contains a couple of actions. One action is responsible for changing rate. Another one is responsible for uploading a file.
the actions work correctly when I play with them. but as soon as I upload a file, if I try to change the rate the post action fails because the url it tries to post to lack the controller name in it. Here are the codes.
here is my code in the view:
Change rate:
<form method="post" action="#Url.Action("UploadPreparedContract")">
#Html.Hidden("userApplicationId", Model.UserApplicationId)
<div class="upload-section k-content">
#Html.Kendo().Upload().Name("files")
<input type="submit" value="Submit"/>
</div>
</form>
<script type="text/javascript">
jQuery(function($) {
var viewModel = kendo.observable({
currentDisclosedRate: "#Model.CurrentDisclosedRate",
changeRate: function(e) {
e.preventDefault();
var self = this;
var rawValue = $('#newDisclosureRate').val();
var rate = parseFloat(rawValue);
$.ajax({
type: "POST",
url: 'ChangeDisclosureRate',
data: { newRate: rate, userApplicationId: #Model.UserApplicationId},
}).done(function(result) {
Notification.success('Rate changed');
self.set("currentDisclosedRate", rawValue);
})
.fail(function(err) {
Notification.error('Not changed. Customer may have placed order');
});
},
});
kendo.bind($("#page"), viewModel);
});
and here is the controller
public class ContractPreparationController : Controller
{
// GET: Application/ContractPreparation
public ActionResult Index(int userApplicationId)
{
// logic to prepare model
return View(new ContractPreparationOutputModel()
{
// Model properties
});
}
[HttpPost]
public async Task<ActionResult> ChangeDisclosureRate(decimal newRate, int userApplicationId)
{
return await Command.ApplyAsync(new ChangeDisclosureRateCommand() {UserApplicationId = userApplicationId, NewDisclosureRate = BasisPoint.Percent(newRate) }) == Command.CommandResult.Succeeded
? new HttpStatusCodeResult(HttpStatusCode.OK)
: new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
[HttpPost]
public async Task<ActionResult> UploadPreparedContract(IEnumerable<HttpPostedFileBase> files, int userApplicationId)
{
if (files == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
if (files.Count() != 1)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "You must upload one file only");
var application = applicationRepository.GetUserApplication(userApplicationId);
if (application == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid user");
var file = files.Single();
var memberDocument = new MemberDocument(blobService, application.FK_UserId);
await memberDocument.Uploadfile(file);
if (await Command.ApplyAsync(new UploadPreparedContractCommand() {FileGuid = memberDocument.FileGuid , UserApplicationId = userApplicationId, FileExtension = memberDocument.FileExtension}) == Command.CommandResult.Succeeded)
{
return RedirectToAction("Index", new {userApplicationId});
}
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError); // No expected failure case
}
}
Use the Url.Action helper method to generate the correct relative url to the action method.
url: '#Url.Action("ChangeDisclosureRate","ContractPreparation")',
When razor executes the code for your view, it will run the Url.Action method and output the correct url (which will have the controller name if needed). You can see it if you do view source of the page.
Try adding the controller name to your ajax url parameter:
url: 'ContractPreparation/ChangeDisclosureRate'
Otherwise MVC doesn't know what controller to use.

How to pass a object from ajax to the server in MVC?

I've got two models, there are.
public class CreateAssignmentViewModel {
...
public List<CreateAssignmentSelectedItem> SelectedItems { get; set; }
}
public class CreateAssignmentSelectedItem {
...
}
Now I've got a view where contains CreateAssignmentViewModel, as you can see above this class contains a property where is a List<CreateAssignmentSelectedItem>
#model Contoso.MvcApplication.Models.Assignment.CreateAssignmentViewModel
#{
ViewBag.Title = "Create Assignment";
...
}
#using (Html.BeginForm()) {
...
}
Inside of the Html.BeginForm, I've got a partial view. And in it I've got a button using ajax where updates the partial view.
Look the following events. Where says data: I do not know what to enter to access only the property SelectedItems
var addQuestionToAssignmentContent = function (questionId)
{
$.ajax({
url: "/Assignment/AddItemToAssignmentContent",
type: "post",
data: { model: $(this).serialize() /* HERE I DON'T KNOW TO ACCESS THE */, itemId: questionId },
success: function (response) {
var $target = $("#assignmentContent");
var $newHtml = response;
$target.replaceWith($newHtml);
}
});
};
public ActionResult AddItemToAssignmentContent(List<CreateAssignmentSelectedItem> model, string itemId)
{
...
PartialView(..);
}
How can I do to pass only the object in the method?
First, give your form an ID:
#using (Html.BeginForm("actionName", "controllerName", FormMethod.Post, new{id = "frmUpdate"})) {
Second, change your code to be like this:
var f = $("#frmUpdate");
$.ajax({
url: f.attr('action'),
type: f.attr('method'),
data: f.serialize(),
//etc..
I use this in most cases and it works just nice. The data should automatically be bound to the model you have in your update action. So, for example... if you have a #model of type MyModel then in the update action it should look something like this:
[HttpPost]
public ActionResult Update(MyModel updatedModel)
Sometimes I work with a front end guy and he might not adhere to pass in the correct model, he might change the form fields or whatever. In this case I just let him serialize the form and pass it to the action an any way he wants.
I then use the FormCollection object to get the data I need.
Your json call
var addQuestionToAssignmentContent = function (questionId)
{
$.ajax({
url: "/Assignment/AddItemToAssignmentContent",
type: "post",
data: { model: $(this).serialize() /* HERE I DON'T KNOW TO ACCESS THE */, itemId: questionId },
success: function (response) {
var $target = $("#assignmentContent");
var $newHtml = response;
$target.replaceWith($newHtml);
}
});
};
Get a form collection object
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddItemToAssignmentContent(FormCollection collection)
{
vars someValue = collection.GetValue("somefield").AttemptedValue;
}
But if I would have control of front-end as you do then as Matt suggested you should use an pass a model;

Populating A Select List Dynamically MVC

I am attempting to create a cascading dropdown. My Controller looks like this to initialize the view..
public ActionResult Create()
{
var model = new RoundDetailViewModel();
model.AvailableFacilities = new SelectList(db.Facilities, "FacilityId", "Facility_Name");
model.AvailableCourses = new SelectList(Enumerable.Empty<Course>(), "CourseId", "Course_Name");
model.AvailableTeeTypes= new SelectList(Enumerable.Empty<TeeType>(), "TeeTypeId", "Name");
return View(model);
}
This will populate the first dropdown, and also create 2 empty dropdown as expected.
Now on a selection of the first dropdown I want to call an Action in my controller to populate the second dropdown. This is where I am a little foggy on the code in the action to populate the second dropdown. I want to use something like this to trigger calling the action..
$("#ddlFacility").change(function () {
var selectedFacility = $(this).val();
if (selectedFacility != null && selectedFacility != '') {
$.getJSON("#Url.Action("GetCourse")", { facility: selectedFacility }, function (courses) {
var coursesSelect = $('#ddlCourse');
coursesSelect.empty();
$.each(courses, function (index, course) {
coursesSelect.append($('<option/>', {
value: course.value,
text: course.text
}));
});
});
}
});
public ActionResult Courses(int facilityId)
{
//WHAT GOES HERE TO POPULATE SELECT LIST??
}
Need to return JsonResult and allow Get (if that is what you decide to do) Alternatively you need to make it a POST and do a POST to it.
Something like
public JsonResult GetCourses(int id)
{
var Courses= db.Courses.Where(a=>facilityId==id).ToList();
SelectList list = new SelectList(Courses,"Text", "Value");
return Json(list , JsonRequestBehavior.AllowGet);
}

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

Resources