Displaying a JSON in a table ASP.Net MVC - asp.net

I have a JWT that I want to display its contents after signature validation. so, I verify the signature like this:
var verified = JWT.Decode(token, publicKey);
In this case, verified is a string containing the JSON payload, looks something like this:
{"sub":"211668914321303","aud":"MUSCA","ver":"1.0.0","nbf":1544459925,"iss":"blimp gmbh","rle":"MUSCA_ACCESS","prm":"This chunk is bound to the something for blimp gmbh","exp":4703150773,"iat":1544459925,"jti":"46"}
now to view this on a page in form of a table, it's easier to send it as a JSON, and loop on its Type and Value, like this:
var verifiedJSON = JsonConvert.DeserializeObject(verified); //convert to JSON
ViewBag.payload = verifiedJSON
in my view, I loop on the ViewBag like this
<table class="table-bordered table-responsive">
#foreach (var line in ViewBag.payload)
{
<tr>
<td>#line.Type</td>
<td>#line.Value</td>
</tr>
}
I would expect that the table will show type and value in columns, but instead of the type(key), I get the word Property, with the expected value in each row!
I tried to display the JSON below the table to see if it came with "property" in the key fields, but it viewed with the correct key names. am I missing something or why can't I get the table to view the keys correctly?

I found the issue thanks to jabberwocky
I copied a table that was looping on an IEnumerable, and changed the variable which is a JSON, and by referring to Json.NET Documentation, I found out that the Key is not called Key or Type. It is called Name

Related

Datasource Paging Issue (Revised Again)

See Datasource Paging Issue (Revised)
for the original question.
Markus, you were kind enough to help with out with the issue of incorporating a record count into a query using a calculated datasource. I have a search form with 15 widgets - a mix of date ranges, dropdowns, text values and ._contains, ._equals, ._greaterThanOrEquals, ._lessThanOrEquals, etc.
I have tested this extensively against mySQL SQL code and it works fine.
I have now added a 16th parameter PropertyNames, which is a list with binding #datasource.query.filters.Property.PropertyName._in and Options blank. The widget on the form is hidden because it is only used for additional filtering.
Logic such as the following is used, such that a particular logged-in user can only view their own properties. So if they perform a search and the Property is not specified we do:-
if (params.param_Property === null && canViewAllRecords === false) {
console.log(params.param_PropertyNames); // correct output
ds.filters.Property.PropertyName._in = params.param_PropertyNames;
}
The record count (records.length) is correct, and if I for loop through the array of records the record set is correct.
However, on the results page the table displays a larger resultset which omits the PropertyNames filter. So if I was to search on Status 'Open' (mySQL results 50) and then I add a single value ['Property Name London SW45'] for params.param_PropertyNames the record count is 6, the records array is 6 but the datasource display is 50. So the datasource is not filtering on the property array.
Initially I tried without adding the additional parameter and form widget and just using code such as
if (params.param_Property === null && canViewAllRecords === false) {
console.log(params.param_PropertyNames); // correct output
ds.filters.Property.PropertyName._in = properties; // an array of
properties to filter out
}
But this didn't work, hence the idea of adding a form widget and an additional parameter to the calculated recordcount datasource.
If I inspect at query.parameters then I see:-
"param_Status": "Open",
"param_PropertyNames": ["Property Name London SW45"],
If I inspect query.filters:-
name=param_Status, value=Open
name=param_PropertyNames, value=[]}]}
It looks as though the filter isn't set. Even hard coding
ds.filters.Property.PropertyName._in = ['Property Name London SW45'],
I get the same reuslt.
Have you got any idea what would be causing this issue and what I can do for a workaround ?
Using a server side solution I would suggest editing both your SQL datasource query script (server side) that is supposed to filter by this property list and including the same code in your server side script for your calculated Count datasource. The code would look something like this, not knowing your exact details:
var subquery = app.models.Directory.newQuery();
subquery.filters.PrimaryEmail._equals = Session.getActiveUser().getEmail();
subquery.prefetch.Property._add();
var results = subquery.run();
if(!results[0].CanViewAllRecords) {
query.filters.Property.PropertyName._in = results[0].Property.map(function(i) {return i.PropertyName;});
}
By adding this code you are filtering your directory by your current user and prefetching the Property relation table, then you set the filter only if your user canviewallRecords is false and use JS map function to create an array of the PropertyName field in the Property table. As I stated, your code may not be exactly the same depending on how you have to retrieve your user canviewallrecords property and then of course I don't know your relation between user and Property table either, is it one-to-many or other. But this should give you an idea how to implement this on server side.

2sxc Relationship Filter without EntityTitle

I'd like to use the relationship filter to filter for a tags. This works fine when I pass a text string and it can search the EntityTitle, but I'd like to pass an entity_id to the filter.
I noticed in the details of the query results that the relationship filter has a "CompareAttribute=EntityTitle". Is there a way to edit that to make it EntityID?
Thanks.
At the moment filtering by a different property in related items could only be done in code. Heres' how
create your visual query as you would, with the only "mistake" being the wrong field
in your razor template, access the Query with var q = App.Query["queryname"];
then before you get the data, change the CompareAttribute. This will take a bit of fiddling about, because you have to cast the q from before to a IDataTarget and navigate up the query-tree like var relFilter = q.In["Default"].Source, then cast that again to the right type, and then change the relFilter.CompareAttribute = "Country";
...something like that :)
Afterwards you can access the query results with a foreach(var x in AsDynamic(q["Default"])) {...}
I've tried this where I wanted some locations grouped by region,
So I did:
region 1
address 1
address 2
region 2
address 3
address 4
So basically it should do the same except it would query by region.
So this is what I came up with:
#{
var someAddresses = App.Query["FilterAddresses"]["ListContent"];
someAddresses.Filter = region.RegionName;
Data.In.Add("someAddresses", someAddresses["Default"]);
}
#foreach (var pc in AsDynamic(someAddresses.List)) {
<li>#pc.Naam</li>
}
However it says:
CS1061: ToSic.Eav.DataSources.IDataStream does not contain a definition for Filter.
So should it be something else?

Linq call using anonymous/dynamic type does work in view

For the life of me I cannot figure out why this works. I'm simply calling some data via link and passing it on to the view. It works when I passe the data directly like this:
var invoices = (
from s in navdb.Sales_Invoice_Header
where s.Salesperson_Code == repCode
where s.Posting_Date > date
select s
).ToList();
But when I create an anonymous type on the fly it does not, like this:
var invoices = (
from s in navdb.Sales_Invoice_Header
where s.Salesperson_Code == repCode
where s.Posting_Date > date
select new {
s.No_,
s.Bill_to_Customer_No_,
s.Bill_to_Name,
s.Salesperson_Code
}
).ToList();
when accessing it like this after:
<table>
#foreach (var invoice in ViewBag.invoices)
{
<tr>
<td>#invoice.No_</td>
<td>#invoice.Bill_to_Customer_No_</td>
<td>#invoice.Bill_to_Name</td>
<td>#invoice.Salesperson_Code</td>
</tr>
}
</table>
I just get an: 'object' does not contain a definition for 'No_'
I've tried adding No_ = s.No_ and so forth, that does not help either. What am I doing wrong?
Duplicate question here: Dynamic Anonymous type in Razor causes RuntimeBinderException
The reason for this is that the anonymous type being passed in the
controller in internal, so it can only be accessed from within the
assembly in which it’s declared. Since views get compiled separately,
the dynamic binder complains that it can’t go over that assembly
boundary.
Basically, you can't bind against anonymous dynamic objects in this way. Define a specific class or struct to get around this.
In your first example, you're getting a List<whatever s is>, in your second example, you're getting a List<object>. And 'object' does not contain a definition for 'No_'

meteor 0.5.7: how to handle/use Meteor.Collection.ObjectID?

I have updated my meteor yesterday and tried using the new Meteor.Collection.ObjectID.
But since with no success. First i updated my collections in this way:
myCollection = new Meteor.Collection('mycollection', {idGeneration: 'MONGO'}
Now, normal new inserts have an _id like Wi2RmR6CSapkmmdfn... (?)
Then i have a collection with an array included. I like to have an unique id for every object in this array. So i $push an object with a field like id: new Meteor.Collection.ObjectID() into my array. The result in the database is like this: ObjectId("5b5fc278305d406cc6c33756"). (This seems to be normal.)
But later i want to update my pushed object, if the id equals an id, which i stored as data attribute in an html tag before.
var equals = EJSON.equals(dbId, htmlId); (This results every time in false. So i logged the values dbId and htmlId into the console with console.log(typeof dbId, dbId);)
The values of this two variables is as follows:
object { _str: 'a86ce44f9a46b99bca1be7a9' } (dbId)
string ObjectID("a86ce44f9a46b99bca1be7a9") (htmlId; this seems to be correct, but why is a custom type a string?)
How to use the Meteor.Collection.ObjectID correct?
When placing your htmlId in your html you need to put it in as a string and not as an object, remember _id is an object now, handlebars is guessing and using toString() & thats why it shows up as ObjectID("...").
So if you're using {{_id}} in your html you now need to use {{_id.toHexString}} to properly extract the string part of it out
When you extract this html value with your javascript you need to make it back into an objectid:
js:
var valuefromhtml = "a86ce44f9a46b99bca1be7a9"; //Get with Jquery,DOM,etc
htmlId = new Meteor.Collection.ObjectID(valuefromhtml); //see: http://docs.meteor.com/#collection_object_id
EJSON.equals(htmlId, dbId); //Should be true this time

Getting the values from a repeated Embedded Schema using TOM.NET API in an event handler in SDL Tridion 2011 SP1

I am working on the Event Handler for saving a component.
My objective is to perform some validations when the user creates and component based on a schema.
I have a schema with the name "Employee".
Employee has an embedded schema with the name "Experience" and it is multivalued.
Experience has 3 fields.
Role : Drop down with the values Manager, Lead.
Company: Text field
Years: Text field
When the user enters some data in these fields, I want to do some validations before save.
The high level design would look like this.
Load the instance of the Component
Navigate to embedded field "Experience"
For every "Experience". I need to get the value of the "Role", and check that appropriate value is entered in other two fields(By writing Component Save event)
For( all the repeated "Experience")
{
If (Role=="Manager")
check the values in the other two fields and do some validation
If (Role=="Lead")
check the values in the other two fields and do some validation
}
I am stuck at extracting the value and Names of subfields at the embeddded field.
I have tried:
Tridion.ContentManager.Session mySession = sourcecomp.Session;
Schema schema= sourcecomp.Schema;
if(schema.Title.Equals("Employee"))
{
var compFields = new ItemFields(sourcecomp.Content, sourcecomp.Schema);
var embeddefield = (EmbeddedSchemaField)compFields["Experience"];
var embeddedfields = (IList<EmbeddedSchemaField>)embeddefield.Values;
foreach(var a in embeddedfields)
{
if(a.Name.Equals("Role"))
{
string value=a.Value.ToString();
}
}
}
Actually I am stuck how to retrieve the values in the other fields at the same time.
Can any one explain how it can be done?
What you need to understand on a EmbeddedSchemaField class is that it represents both a schema and a field (as the name implies...)
I always find it helpful to look at the source XML of the component when writing code that targets its fields, you get a good visual representation of what your classes must do. If you look at a component XML like this:
<Content>
<Title>Some Title</Title>
<Body>
<ParagraphTitle>Title 1</ParagraphTitle>
<ParagraphContent>Some Content</ParagraphContent>
</Body>
<Body>
<ParagraphTitle>Title 2</ParagraphTitle>
<ParagraphContent>Some more Content</ParagraphContent>
</Body>
</Content>
Body is your embedded Schema field, which is multivalued, and contains 2 single-valued fields within it.
Addressing these fields in TOM.NET then:
// The Component
Component c = (Component)engine.GetObject(package.GetByName(Package.ComponentName));
// The collection of fields in this component
ItemFields content = new ItemFields(c.Content, c.Schema);
// The Title field:
TextField contentTitle = (TextField)content["Title"];
// contentTitle.Value = "Some Title"
// Get the Embedded Schema Field "Body"
EmbeddedSchemaField body = (EmbeddedSchemaField)content["Body"];
// body.Value is NOT a field, it's a collection of fields.
// Since this happens to be a multi-valued field, we'll use body.Values
foreach(ItemFields bodyFields in body.Values)
{
SingleLineTextField bodyParagraphTitle = (SingleLineTextField)bodyFields["ParagraphTitle"];
XhtmlField bodyParagraphContent = (XhtmlField) bodyFields["ParagraphContent"];
}
Hope this gets you started.

Resources