Linq call using anonymous/dynamic type does work in view - asp.net

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_'

Related

Displaying a JSON in a table ASP.Net MVC

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

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?

SDL Tridion GetListKeywords using Anquilla Framework

I'm writing a GUI extension and using the Anquilla framework to get a list of Keywords within a Category. I'm obtaining an XML document for the list of keywords then working with that document within my extension.
My problem is that the returned XML doesn't contain the Keyword's 'Description' value. I have the Title and Key etc.
My original code looks like this:
var category = $models.getItem("CATEGORYTCMID:);
var list = category.getListKeywords();
list.getXml();
A typical node returned is this:
<tcm:Item ID="tcm:4-1749-1024"
Type="1024" Title="rate_one" Lock="0" IsRoot="true"
Modified="2012-12-17T23:01:59" FromPub="010 Schema"
Key="rate_one_value" IsAbstract="false"
CategoryTitle="TagSelector"
CategoryID="tcm:4-469-512" Icon="T1024L0P0"
Allow="268560384" Deny="96" IsNew="false"
Managed="1024"/></tcm:ListKeywords>
So I've tried using a Filter to give me additional column information:
var filter = new Tridion.ContentManager.ListFilter();
filter.columns = Tridion.Constants.ColumnFilter.EXTENDED;
var list = category.getListKeywords(filter);
Unfortunately this only gives the additional XML attributes:
IsShared="true" IsLocalized="false"
I'd really like the description value to be part of this XML without having to create a Keyword object from the XML. Is such a thing possible?
cough any ideas? cough
I'm afraid you'll have to load the Keyword itself to get the Description.
It's not used in any lists, so it's not returned in the XML.
You could always create a List Extender to add this information to the list, but try to be smart about it since this extender will execute everytime a GetList is called.
Won't save you from having to open every keyword in the list, but you'll be doing it server-side (with Core Service/NetTcp for instance) which will probably be easier and faster than opening each keyword with Anguilla.
In this instance I only need the one keyword, so I simply get it from the CMS. Getting an object in Anguilla is a bit weird, here's the code:
In your main code area:
var selectedKy = $models.getItem("TcmUriOfKeywordHere");
if (selectedKy.isLoaded()) {
p.selectedKy = selectedKy;
this.onselectedKyLoaded();
} else {
$evt.addEventHandler(selectedKy, "load", this.onselectedKyLoaded);
selectedKy.load();
}
It's worth noting how I store the keyword in the properties of the item, so I can obtain it in the onselectedKyLoaded function
The function called once the item is loaded
ContentBloom.ExampleGuiExtension.prototype.onselectedKyLoaded = function (event) {
var p = this.properties;
var selectedDescription = p.selectedKy.getDescription();
// do what you need to do with the description :)
};
I resolved this, thanks to the answer here: https://stackoverflow.com/a/12805939/1221032 - Cheers Nuno :)

Cannot implicitly convert type to List<string>

Afternoon,
I am getting the following error, and cant work out why... Can some one please take a look and let me know where i am going wrong.
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'
below is what i am trying to use, to get a list back so i can use it with Amazon. I have tried to remove the .ToList() bit but nothing seems to work. I am calling an MS SQL view "GetASINForUpdateLowPrices" which returns a list back of product ASIN's
List<string> prodASINs = dc.GetASINForUpdateLowPrices.ToList();
SQL for the view i am using, this may help a little bit more.
SELECT asin
FROM dbo.aboProducts
WHERE (asin NOT IN
(SELECT aboProducts_1.asin
FROM dbo.aboProducts AS aboProducts_1 INNER JOIN
dbo.LowestPrices ON aboProducts_1.asin = dbo.LowestPrices.productAsin
WHERE (dbo.LowestPrices.priceDate >= DATEADD(day, - 1, GETDATE()))))
What data type is a single ASIN?
Probably your GetASINForUpdateLowPrices is not an IEnumerable<string>. Try this to confirm:
List<string> prodASINs = dc.GetASINForUpdateLowPrices
.Select(e => e.ToString())
.ToList();
When you call your GetASINForUpdateLowPrices, it wont directly return List<string> even if there is only one field in your view. Try the following approach:
List<string> prodASINs = dc.GetASINForUpdateLowPrices
.Select(item => item.AsinFieldName)
.ToList();
Visual Studio IntelliSense should suggest you the property name after typing item.. If the property is not string try to add .ToString() at the end of the property name.
Edit: After your comment, it seems like you need to use it as .Select(item => item.asin.ToString()).
Just use var.
var prodASINs = dc.GetASINForUpdateLowPrices.ToList();
Are you sure that GetASINForUpdateLowPrices.ToList() creates a List of Strings? My best estimation is that it is a generic list of a different type.
To figure out what is going on - Change List<string> prodASINS to be Object obj. Then set a breakpoint to see what List type is actually generated by your ToList() code by checking out the object using the debugger. You can then update your code to move the values into a list of the appropriate type.
You might have to cast the right side of the assignor like this to ultimately get the job done (replacing string with another type if necessary) - List<string> prodASINs =(List<string>)dc.GetASINForUpdateLowPrices.ToList()

MVC3 Razor Dynamic creating forms

First I want to point out Im very new to this area ^_^ .
Anyhow
I'm doing a project with a grade-reporting system and now Im currently working with the cshtml for the update of usertask.
My problem is
I'm currently working with trying to add a form post but it's not really working at all.
To my view I have a model with a list of usertasks (and other data). These usertasks contains int id and string grade.
What I am trying to do is foreach usertask create a form with their own submit button.
The form would have a hidden field for id and a textbox for grading.
The problem i have with #Html.Hidden
Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Hidden' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
Which I assume is you cant dynamicaly use Html.Hidden (i use a foreach loop over the list of usertasks).
Anyone have any idea how to solve this? I've been thinking about creating partialviews but couldnt figure out if you could pass paramters to them. Or make the submit button redirect to a controller-view which takes the 2 paramters?
Anyone have any examples?
Thx for help!
Submitting some code I have been trying to get to work.
foreach (var tskModel in tskgrpModel.usertasks)
{
#tskModel.name
using (Html.BeginForm("Update", "Teacher", FormMethod.Post))
{
if(tskModel.isSet == true)
{
#Html.Hidden("ut_id", tskModel.id )
#Html.TextBox("ut_grade",(string)#tskModel.grading);
}
else
{
#Html.Hidden("ut_id", -1)
#Html.Hidden("ut_id", "NotSet")
}
}
}
Is taskModel.id a dynamic variable?
if so change the line
#Html.Hidden("ut_id", tskModel.id )
to
#Html.Hidden("ut_id", (int) tskModel.id )

Resources