Json method returns Object literal, expected JSON - asp.net-mvc

I'm trying to return JSON, to map to a model, however I can't figure out why my method is returning an object literal and not JSON.
I threw together a Fiddle that shows the current format I am receiving data.
Controller method
public JsonResult GetDeferredAccountDetailsByAccount(int id)
{
var details = _deferredAccountDetailsService.GetDeferredAccountDetailsByAccount(id);
return Json(details, JsonRequestBehavior.AllowGet);
}
This returns :
..And in the browser :
In the Fiddle I linked, simply wrapping the object literal in [] allows Knockout to interpret the object just fine, but without it fails.
Is there something I am doing incorrectly or a reason why I'm not receiving JSON? Do I need to return an ICollection or something for it to be interpreted as JSON?
I looked around but couldn't really find anything.

You expects an Array, but you are returning a literal object at the controller. And you are biding a collection using knockout, but accounts It's a literal. That's why everything works when you put [] at the JSON.
You should just push every property from the JSON to an Array instead of _map, or fix the _map function to bind property to an Array!

Related

For each in by reference or value

I have the following code:
dim key
for each key in Request.Querystring
'do something
key = sanitized_param(key)
next
My question for you classic-asp connoisseur, does classic-asp, or asp in general, pass the variables as references(memory), or by value? Trying to figure out if I sanitize the key variable and pass it back to itself, is it just "alive" for that loop, or does the new value get passed to the original QueryString?
Request.QueryString retrieves the query string parameters by value from the page headers.
You can only make changes to a query string once its been retrieved via Request.QueryString, but you can't make changes directly to Request.QueryString as it's read-only (If you could make changes you would presumably use Response.QueryString, but this isn't a valid response command).
I'm guessing you're trying to sanitize all your query strings in one go? This isn't really possible or indeed necessary. You would typically sanitize a query string as and when you request it:
Response.Write(sanitized_param(Request.QueryString("myQS")))
Or to assign the query string to a variable first then sanitize it:
Dim myQS
myQS = Request.QueryString("myQS")
myQS = sanitized_param(myQS)
' or
myQS = sanitized_param(Request.QueryString("myQS"))
Once the query string has been assigned to a variable and sanitized you're able to reference that variable as often as you like without having to pass it to your sanitize function again.
Also, your example code doesn't make much sense. The key value in your for each loop is referencing just the names of your query strings and not their values. If Response.QueryString was a valid ASP command you would do:
Response.QueryString(key) = sanitized_param(Request.QueryString(key))
But again, this isn't possible.
EDIT: This solution might be what you're looking for:
Create a dictionary object, call it "QueryString" for example. Loop through all your query strings and add a sanitized version to the dictionary object.
Dim QueryString : Set QueryString = Server.CreateObject("Scripting.Dictionary")
For Each Item In Request.QueryString
QueryString.Add Item,sanitized_param(Request.QueryString(Item))
next
Now, to retrieve a sanitized version of a query string just use:
QueryString.Item("query_string_name")
Or for the original unsanitized version you could still use:
Request.QueryString("query_string_name")
Just like Request.QueryString, the dictionary object is forgiving and won't return an error if you ask for a query string that doesn't exist.
You could also create a function for retrieving sanitized query strings, for example:
Function SanitizedQS(ByVal qsName)
SanitizedQS = sanitized_param(Request.QueryString(qsName))
End Function
And rather than using Request.QueryString("query_string_name") just use SanitizedQS("query_string_name").

Fitnesse-Fixture for method that return data types like data set

I am trying to test the .NET class using fitnesse. Is it possible with fitnesse to test a method that returns (not a single value either string or integer) a data set.
Yes. For example, you can return a json object and view the contents of the json object as a return value of a function.
|script |className |
| $variable= |functionName|
For a json object returned, you should be able to view its contents assigned to $variable.
The steps associated with other data structures are similar
A dataset return value is automatically handled with an Array fixture:
|SomeClass|
|SomeMethodThatReturnsDataSet|
|column1|column2|column3|
|a|b|c|
|d|e|f|
See http://fitsharp.github.io/Fit/FixtureWrapper.html for more examples.
You can return IEnumerable class and check the table.
See whole tutorial there: https://github.com/imanushin/NetRunner/wiki/Net-Runner-tutorial

Angular http get returns string in extra quotes

My project is a mixture of AngularJS and ASP.Net MVC.
I am using $http.get to call my api controller, which returns a string. But in the .success function, the data is getting returned like so "/"my string/"" (with the extra quotes around it).
Why are those there, and how can I get rid of them?
I need them gone because I am setting the title attribute of an element to the string that is returned, and this makes the tooltip have quotes around it (ugly).
parse the string value using JSON.parse(variable)

How to encode javascript string for display and post back?

I have an MVC application that is rendering rendering the following javascript on the client:
var rawData = [{"ID":5317,"Code":"12345","Description":"sometext \u003c/= 100"}];
The JSON data is a result of serializing an object using the JavaScriptSerializer and then running the result through the Html.Raw() helper.
This data is then used to load a knockout view model and display a popup on hover. In the popup, only the "sometext" portion of the "Description" property is being shown as the string gets converted to the unencoded version when setting the rawData variable (i.e. \u003c is converted to <).
Also, this data ends up being sent back to the server upon saving of data, and the ASP.NET validation kicks in and fails the request as it detects the "
I've worked around this, temporarily, by adding a computed property to my Knockout View Model like so:
self.DescriptionEncoded = ko.observable('');
self.Description = ko.computed({
read: function() {
return self.DescriptionEncoded ();
},
write: function(value) {
self.DescriptionEncoded($('<div/>').text(value).html());
}
});
In this way I can access the escaped property from my popup and the unescaped value is not sent back to the server when I serialize my viewmodel (using .toJSON()).
Is there a more global way to handle this rather than creating computed properties for every object that may have some text that appear to be a bad request while not compromising on security? I've considered an overload/helper to the serialization routine that would accept a list of properties to apply a Find/Replace I am thinking this will have to be handled on a case by case basis in a manner similar to what I've already done. As for sending the data back to the server, I could override the toJSON() method on my view model and delete the properties that don't need to be sent back, but that won't help me with my popup.
Thoughts?
You can encode using Ajax.JavaScriptStringEncode. You might also get the AntiXSS library and use it for the encoding.
I hope I understood your question well.

Traverse through JSon data in dart?

After trying a lot sorry for asking such a trivial question.
Given below screenshot consist of a data that I have successfully received from the server.
I would like to know how to traverse through the data since whenever I try to cast it to something and try a foreach it gives an error.
The actual data sent from the server is a List() type.
I want to know how to cast it to same type and use it here.
I tried casting but it says unexpected token here.
Any help is appreciated.
JSON.parse return type depends on the String you try to parse. See the doc :
Parses json and build the corresponding parsed JSON value.
Parsed JSON values are of the types num, String, bool, Null, Lists of parsed JSON values or Maps from String to parsed JSON values.
From your screenshot, it seems that the return value is a List. You can do something like the following to use it (did you notice the typo in your commented code - .fore) :
final parsedList = JSON.parse(e.data)/*.fore*/;
parsedList.forEach((x){
query('#idData').appendText(x[0]);
query('#idData').appendText(x[1]);
query('#idData').appendText(x[2]);
query('#idData').appendText(x[3]);
});

Resources