ASP.Net MVC Post: key-value pairs - can't retrieve value - asp.net-mvc

I have a forms collection (fc) and I'm attempting to append to an email the values of the 'key' and the 'value'. I'm having no problem with the key (newKey), but I can't seem to code the 'value' properly. The 'for' loop checks to see if the key's first 3 characters of the key are 'ddl' indicating it came from a dropdownlist. if so, the loop should append the value from the dropdownlist control (the value of the key-value pair). (If not, the loop calls another method to append either a yes or no based upon the value of a checkbox control) Thanks in advance.
//Append new key-value pairs implemented since legacy keys
for (int i = 0; i < newKeys.Length; i++ )
{
//Checks for prefix of element to determine type of element
if(newKeys[i].Substring(0,3) == "ddl"){
sb.Append(newKeys[i] + ": " + fc.GetValue(newKeys[i]) + "\",<br />");
sb.Append(newKeys[i] + ": " + fc.GetValues(newKeys[i].ToString()) + "\",<br />");
}
else{
sb.Append(newKeys[i] + ",\"" + Boolean(fc[newKeys[i]]) + "\",<br />");
}
}
The 2 sb.append commands return the following:
ddlStratacacheConstellationManagerRole: System.Web.Mvc.ValueProviderResult"
ddlStratacacheConstellationManagerRole: System.String[]",

The values you are writing aren't of type String, nor they have the ToString() method overridden.
That's why the standard object.ToString() method is called and the object's type name is appended to the string.
To remedy this, you either need to override the ToString() method of all possible form collection's values, or think of some other algorithm, which would iterate over collections and output the corresponding values.

Related

Neo4j Custom Procedures: How to pass query parameters?

I am trying to write a custom procedure for the Neo4J GraphDB in accordance to the documentation and the there referenced template. The procedure should ultimately generate a graph projection using the GDSL, for nodes with a certain label that is provided as a procedure parameter. For this it is, of course, necessary to pass the label to the query that is to be executed within the custom procedure. However, I cannot seem to find out how to pass parameters to a query string.
#Procedure(value = "custom.projectGraph")
#Description("Generates a projection of the graph.")
public Stream<ProRecord> projectGraph(#Name("graph") String graph) {
Map<String, Object> params = Map.of (
"graph", graph
);
return tx.execute("call gds.graph.project.cypher(\"$graph\", "
+ "\"MATCH (n:$graph) return id(n) as id\", "
+ "\"MATCH (src:$graph)-[]-(dst:$graph) "
+ "RETURN id(src) AS source, id(dst) AS target\") "
+ "YIELD graphName", params)
.stream()
.map(result -> (String) result.get("graphName"))
.map(ProRecord::new);
}
public static final class ProRecord {
public final String graphName;
public ProRecord(String graphName) {
this.graphName = graphName;
}
}
This code, unfortunately, does not work as intended, throwing the following exception:
Invalid input '$': expected an identifier
I did copy the syntax of prefixing placeholders with $-characters from other examples, as I could not find any hints on the passing of query parameters in the JavaDoc of the library. Is this even the correct documentation for custom neo4j procedures? Am I possibly using the wrong method here to issue my queries? It'd be very kind if someone could lead me into the right direction on that matter.
In general, when you use a string parameter the $param is automatically quoted, unlike the String.format for example.
Therefore there are 2 problems in your query:
\"$graph\" : in this case you are doubly quoting the parameter, try to write only $graph instead
Things like this n:$graph cannot be done unfortunately, the neo4j parameter handling is not able to recognize where to quote and where not, so you could use String.format or concat string with parameters (e.g. "MATCH (n:" + $graph + ") return id(n)...").
So, in short, this piece of code should work in your case:
return tx.execute("call gds.graph.project.cypher($graph, " +
"'MATCH (n:' + $graph + ') return id(n) as id', " +
"'MATCH (src:' + $graph + ')-[]-(dst:' + $graph + ') RETURN id(src) AS source, id(dst) AS target') YIELD graphName",
params)
.stream()
.map(result -> (String) result.get("graphName"))
.map(ProRecord::new);

How to loop through array and do something different with the last index?

So, I have an array of Genre objects that contain name properties that are Strings that I want a UILabel.text to contain the values.
Id like the label to look something like this ultimately:
String[0] / String[1] / String[2] / String[3]
with the front slashes separating the strings. I can get the label to contain the values, and I can even get the / separators, but my logic is flawed as after the last string I still get the front slash.
Can someone help me with the logic where it will add the string and a front slash unless its the last item in the array and if its the last item in the array it should just add the string and no front slash.
Right now I just have a simple For In loop
if game?.genres?[0].name == nil {
for genre in game!.genres! {
genreLabel.text! += "\(genre.name!) / "
}
}
Ive tried
game.genres.map { $0.name }.joined(separator: “ / “)
but that gives an error of:
Value of type '[Genre]' has no member 'name'
What is the logic here to do something different with the last element?
Because you didn't use enough questions marks, the compiler thinks you're using Optional.map instead of Array.map.
You can get the result you want, like this:
game?.genres?.compactMap(\.name).joined(separator: " / ")
…but you're using too many optionals. Just make genres non-optional. An array can already be empty. It doesn't also need to be nil.
game?.genres.compactMap(\.name).joined(separator: " / ")
…and if you can, make game and name non-optional too.
game.genres.map(\.name).joined(separator: " / ")
You can achieve this by using index
for (index,genre) in game!.genres!.enumerated() {
genreLabel.text! += "\(genre.name!) / "
if index == game!.genres!.count -1 {
// do something with last index
}
}

document.querySelectorAll to find item based on multiiple condition?

I am trying to find div based on combination of class name and data attribute. But it doesn't seems to returning any value.
var products = document.querySelector("div.productDisplay").querySelectorAll("[data-productId='" + id + "']");
The first .querySelector() call will only return a single element. If that does not match the selector in the querySelectorAll() call then no elements will be retured.
Try using a single selector, such as:
var products = document.querySelector("div.productDisplay[data-productId='" + id + "']");

help with oauthService and linkedin

I am trying to iterate over a list of parameters, in a grails controller. when I have a list, longer than one element, like this:
[D4L2DYJlSw, 8OXQWKDDvX]
the following code works fine:
def recipientId = params.email
recipientId.each { test->
System.print(test + "\n")
}
The output being:
A4L2DYJlSw
8OXQWKDDvX
But, if the list only has one item, the output is not the only item, but each letter in the list. for example, if my params list is :
A4L2DYJlSwD
using the same code as above, the output becomes:
A
4
L
2
D
Y
J
l
S
w
can anyone tell me what's going on and what I am doing wrong?
thanks
jason
I run at the same problem a while ago! My solution for that it was
def gameId = params.gameId
def selectedGameList = gameId.class.isArray() ? Game.getAll(gameId as List) : Game.get(gameId);
because in my case I was getting 1 or more game Ids as parameters!
What you can do is the same:
def recipientId = params.email
if(recipientId.class.isArray()){
// smtg
}else{
// smtg
}
Because what is happening here is, as soon as you call '.each' groovy transform that object in a list! and 'String AS LIST' in groovy means char_array of that string!
My guess would be (from what I've seen with groovy elsewhere) is that it is trying to figure out what the type for recipientId should be since you haven't given it one (and it's thus dynamic).
In your first example, groovy decided what got passed to the .each{} closure was a List<String>. The second example, as there is only one String, groovy decides the type should be String and .each{} knows how to iterate over a String too - it just converts it to a char[].
You could simply make recipientId a List<String> I think in this case.
You can also try like this:
def recipientId = params.email instanceof List ? params.email : [params.email]
recipientId.each { test-> System.print(test + "\n") }
It will handle both the cases ..
Grails provides a built-in way to guarantee that a specific parameter is a list, even when only one was submitted. This is actually the preferred way to get a list of items when the number of items may be 0, 1, or more:
def recipientId = params.list("email")
recipientId.each { test->
System.print(test + "\n")
}
The params object will wrap a single item as a list, or return the list if there is more than one.

Classic asp: why doesn't Request.QueryString("foo").toString() work in a javascript (JScript) based page?

All I want to do is
Get the name-value pairs that were supplied to Request.QueryString
Populate a javascript object (aka hash) with keys from the names and values from the values
Halt the page if one of the expected hash values is the empty string
The Request.QueryString object is reminding me why I hated classic asp even before it was an abandoned technology. :/
The Request.QueryString collection has an awkward interface, particularly when it comes to iterating or cases where there are multiple params with the same name. I suggest grabbing the whole querystring using Request.QueryString.Item() or Request.ServerVariables('QUERY_STRING') and parse it using unescape/decodeURIComponent. It's a bit of effort, but gives you more control and consistency.
A simple example that lowercases keys:
var params = parseQueryString(Request.QueryString.Item());
function parseQueryString(qs) {
var parsed = {}, pairs = qs.split('&');
for (var i = 0; i < pairs.length; i ++) {
var pair = pairs[i], pos = pair.indexOf('=');
if (pos < 0) pos = pair.length;
parsed[unescape(pair.slice(0, pos)).toLowerCase()] = unescape(pair.slice(pos + 1));
}
return parsed;
}
Querystring contents are treated as string by default i believe..
But if you have to, you can always do String( request.querystring("foo") );
Are the keys known before-hand ? or you want to iterate through the pairs and retrieve both key and value ?

Resources