I have the following #Script (ServiceStack framework) template.
I want to use the argument Code in my dbSelect.
How is this possible?
Example:
<small>Some Label</small><header>{{Code}}</header>
{{ 'select * from TrackingEntity WHERE Barcode = #Barcode' |> to => selectSql }}
{{ selectSql |> dbSelect({Barcode: {Code} }) |> htmlDump({ className: 'table styled-table' }) }}
In this line of code, I am trying to pass the argument to the dbSelect function.
selectSql |> dbSelect({Barcode: {Code} })
dbSelect accepts a JS object literal, try:
selectSql |> dbSelect({ Barcode: Code })
Related
I am getting response like this from the Client :
http://192.168.25.241/CPMO.asmx?wsdl|context=
{transaction_id=9610842, ref_id=14943018526993,
service_id=CPA_MCOM_999, error_list=n/a, sub_eff_date=201705121715460,
notification_ind=2, error_code=1, destination_mobtel=0105660125,
keyword=dummy, CURRENT_STEP=0, sub_exp_date=201705131200000}
Note :
?wsdl|context=
{transaction_id=9610842, ref_id=14943018526993, service_id=CPA_MCOM_999, error_list=n/a, sub_eff_date=201705121715460, notification_ind=2, error_code=1, destination_mobtel=0105660125, keyword=dummy, CURRENT_STEP=0, sub_exp_date=201705131200000}
So How can I read this string within the {}, after reading the parameters, redirect to the particular URL (I mean above URL), Can anyone help me in C#?
You can read the string as explained below.
var text = "{transaction_id=9610842, ref_id=14943018526993, service_id=CPA_MCOM_999, error_list=n/a, sub_eff_date=201705121715460, notification_ind=2, error_code=1, destination_mobtel=0105660125, keyword=dummy, CURRENT_STEP=0, sub_exp_date=201705131200000}";
//replace {} from your string.
var dict = text.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
.Select(part => part.Split('='))
.ToDictionary(split => split[0], split => split[1]);
I have a razor statement:
#{
var renderColumn = new Action<OlapReportColumn>(col =>
{
if (col.IsSpaned)
{
#<th colspan="#col.Columns.Count">#col.Caption</th>;
}
});
}
Here is a code for render html table's header. So while I trying to call view I get an exception (translated from russian) as:
an operator can be used only assignment expression, call, increment, decrement and expectations
Here is a razor generated code part with an error:
var renderColumn = new Action<OlapReportColumn>(col =>
{
if (col.IsSpaned)
{
#line default
#line hidden
item => new System.Web.WebPages.HelperResult(__razor_template_writer => { // ERROR HERE!
BeginContext(__razor_template_writer, "~/Areas/Report/Views/ReportsOlap/ReportTableGenerator.cshtml", 324, 3, true);
Here a part of razor code called renderColumn
<table id="reportGrid">
<thead>
<tr>
#foreach (var h in report.Header)
{
renderColumn(h);
}
</tr>
What I am doing wrong in here?
The Action that you've defined is behaving just like any other method in C#. The line #<th colspan="#col.Columns.Count">#col.Caption</th>; is not simply output to the output stream; instead the compiler is seeing a statement that it doesn't understand resulting in the error:
CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
In order to write the <th colspan="#col.Columns.Count">#col.Caption</th> to the output stream you can use the WriteLiteral method:
var renderColumn = new Action<OlapReportColumn>(col =>
{
WriteLiteral("<th colspan=" + col.Columns.Count + ">" + col.Caption + "</th>");
});
Perhaps more idiomatic than that though would be to use a Func that returns what you'd like to ouptut and then output that at the call site:
var renderColumn = new Func<OlapReportColumn, object>(col =>
{
return Html.Raw("<th colspan=" + col.Columns.Count + ">" + col.Caption + "</th>");
});
The call would then need to be changed very slightly to tell razor that you wish to output the results:
#foreach (var h in report.Header)
{
#(renderColumn(h))
}
Going further, there is built-in support for Funcs of this nature as described in this blog by Phil Haack. Using this method your call stays the same as the call just above but the Func becomes:
Func<OlapReportColumn, object> renderColumn2
= #<th colspan="#item.Columns.Count">#item.Caption</th>;
From Phil Haack's blog
Note that the delegate that’s generated is a Func<T, HelperResult>. Also, the #item parameter is a special magic parameter.
Try changing your HTML table header code from this :
#<th colspan="#col.Columns.Count">#col.Caption</th>;
to this :
#:<th colspan="#col.Columns.Count">#col.Caption</th>;
Insert that colon : immediately after # delimeter and check if this works.
I am trying to read json into my C# application from a url. When I run the application I keep getting a error:
"Additional Text encountered after finished reading JSON content: {. Path ",line 2, position 667".
This is from this URL
I checked the page and the view source and can't seem to find the problem. How do I fix this?
The JSON is derived from a php array that is json encoded and echoed:
$args = array( 'post_type' => 'product', 'posts_per_page' => 200, 'product_cat' => 'Alcopops' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo json_encode($product);
endwhile;
wp_reset_query();
That page doesn't contain valid json. Take a look at this:
"product_type":"simple"}{"id":246,"post":
there's no comma between } and {
Edit:
The problem is with your php, rather than the c#.
Try this:
$args = array( 'post_type' => 'product', 'posts_per_page' => 200, 'product_cat' => 'Alcopops' );
$loop = new WP_Query( $args );
echo json_encode($loop->get_posts());
wp_reset_query();
use WebClient :
var json = new WebClient().DownloadString("http://cbbnideas.com/brydens-website/products/?category=Alcopops");
On an ASP.NET MVC I have a textarea as follows:
#Html.TextAreaFor(x => x.Text, 12, 12, null)
On the controller I am defining the model as:
NewPostModel model = new NewPostModel() { Text = #"First line\r\n\r\nan\[x \cdot y\]" };
But in the view I don't have a break line inside the textbox.
The text appears all in the same line. Why?
UPDATE
I isolated the problem ... If it don't use # before the string then it works.
The problem is that in the string I have parts as [ and ( ...
And then I get an error: Unrecognized escape sequence.
How can I solve this?
Thank You,
Miguel
If you can edit your string, you can do something like this and avoid so the use of the # :
var model = new NewPostModel() { Text = "First line\r\n\r\nan\\[x \\cdot y\\]" };
I'm using Razor to insert items into a Javascript array. It's working as intended using the code below. However, it results in one extra comma at the end of the array. Can someone suggest a way to prevent this from happening?
graphByMonth = new Array(
#foreach (var cost in Model.Cost) {
<text>
[#cost.CPM, '#cost.EndDate'],
</text>
}
);
You should never use any string concatenations when dealing with javascript. If you want to pass some server side model to a javascript variable you could JSON serialize it like this to ensure that dangerous characters are properly escaped:
var graphByMonth = #Json.Encode(Model.Cost.Select(cost => new {
cpm = cost.CPM, endDate = cost.EndDate
}));
which will render as:
var graphByMonth = [
{ cost: '1', endDate: 'date 1' },
{ cost: '2', endDate: 'date 2' },
{ cost: '3', endDate: 'date 3' }
];
Using Json.Encode you ensure that the values are properly encoded and you won't have any broken syntax which is what you will get if you ever try to manually do this by using some string concatenations, foreach loops or whatever.