Adding String to List MVC - html

Im using MVC4 Razor and I just want to ask how to create a List of string and use it in a field seperated by comma? Cause I tried something and it shows an error Cannon implicitly convert type void to object Ill show you my set of codes
declared this on my header:
List<string> elements = new List<string>();
Then I want this List<string> elements to get populated inside a foreach loop
<tbody>
#for (var i = 0; i < Model.Cards.Count; ++i)
{
var counter = i + 1;
<tr>
<td valign="middle">
<p class="small">#counter</p>
</td>
<td valign="middle">
<p class="small">#Model.Cards[i].Number.Substring(0,5)</p>
</td>
#elements.Add(Model.Cards[i].Number.Substring(0,5))
</tr>
}
</tbody>
And use it in a Label
<div> Card Number <p> #String.Join(", ", elements.ToArray()) </p></div>

If you flow #Stephen Muecke, suggestion it's best. But if you flow your process the your code look like:
#{
List<string> elements = new List<string>();
}
<tbody>
#for (var i = 0; i < Model.Cards.Count; ++i)
{
var counter = i + 1;
var cardNumber=Model.Cards[i].Number.Substring(0,5);
elements.Add(cardNumber);
<tr>
<td valign="middle">
<p class="small">#counter</p>
</td>
<td valign="middle">
<p class="small">#cardNumber</p>
</td>
</tr>
}
</tbody>
#{
var str=string.Join(", ", elements.ToArray());
}
<div> Card Number <p> #str </p></div>

Related

iTextSharp HTML to PDF with ParseXHtml unicode characters are not parsed

first of all I've been dealing with this problem for the past 2 days and I thought I may finally ask it since I've not found any working solution. First let me introduce the problem then I'll explain what I've tried.
As the title introduces, I'm trying to convert HTML to PDF using iTextSharp, the HTML includes gridview as well.
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
System.Text.Encoding Enc = System.Text.Encoding.GetEncoding("UTF-8");
iTextSharp.text.pdf.BaseFont STF_Helvetica_Turkish = iTextSharp.text.pdf.BaseFont.CreateFont("Helvetica", "CP1254", iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font fontNormal = new iTextSharp.text.Font(STF_Helvetica_Turkish, 12, iTextSharp.text.Font.NORMAL);
StringReader sr = new StringReader(sw.ToString());
string contentHtml = PrintElem();
contentHtml = contentHtml.Replace("Ş", "S");
contentHtml = contentHtml.Replace("İ", "I");
contentHtml = contentHtml.Replace("ı", "i");
contentHtml = contentHtml.Replace("Ğ", "G");
contentHtml = contentHtml.Replace("Ü", "U");
contentHtml = contentHtml.Replace("ğ", "g");
contentHtml = contentHtml.Replace("ş", "s");
StringReader srHtml = new StringReader(contentHtml);
Stream denemeStream = GenerateStreamFromString(srHtml.ToString());
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10f, 10f, 10f, 0f);
MemoryStream ms = new MemoryStream();
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, ms);
pdfDoc.Open();
using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(PrintElem().ToString())))
{
using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(PrintElem().ToString())))
{
//iTextSharp.tool.xml.XMLWorkerFontProvider fontProvider = new iTextSharp.tool.xml.XMLWorkerFontProvider();
//Parse the HTML
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, msHtml, (Stream)null);
}
}
//iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, denemeStream,null,Encoding.UTF8);
pdfDoc.Close();
MemoryStream ret = new MemoryStream(ms.ToArray());
return ret;
}
}
As you can see last solution I tried was to change all turkish characters to english ones yet the pdf output still did not display them.
So far I've tried changing encoding to everything offered on the internet. I tried to add the font yet I've failed to do it as you can see I'm not using that overloaded function of ParseXHtml (if you know how to add this fontNormal to the parsing I'll gladly try that out as well).
the PrintElem function returns the following HTML content (or similar ones that contain turkish characters inside)
<html>
<head>
<h3 align='center'>ABG SİGORTA ALACAK/VERECEK MUTABAKAT EKSTRESİ</h3>
<style>#ContentPlaceHolder1_vaultsListGridview{width: 100%;} td:nth-child(3) {text-align: right;}td:nth-child(4) {text-align: right;}.netWorthClass{text-align: right;}</style>
</head>
<body >
<br/>
<div>Kasa Adı = 1</div>
<div>Devir = 56 TL</div>
<br/>
<div>
\r\n\t
<table class=\"table table-hover table-striped\" cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"ContentPlaceHolder1_vaultsListGridview\" style=\"border-collapse:collapse;\">
\r\n\t\t
<thead>
\r\n\t\t\t
<tr>
\r\n\t\t\t\t
<th scope=\"col\">Tarih</th>
<th scope=\"col\">Açıklama</th>
<th scope=\"col\">Giren</th>
<th scope=\"col\">Çıkan</th>
\r\n\t\t\t
</tr>
\r\n\t\t
</thead>
<tbody>
\r\n\t\t\t
<tr>
\r\n\t\t\t\t
<td>\r\n <span id=\"ContentPlaceHolder1_vaultsListGridview_fullDateLabel_0\">26/05/2017</span>\r\n </td>
<td>\r\n <span id=\"ContentPlaceHolder1_vaultsListGridview_detailLabel_0\">MART AYI KALAN VE NİSAYIN AYI SSK ÖDEMESİ</span>\r\n </td>
<td>\r\n 0\r\n </td>
<td>\r\n 1,295.00\r\n </td>
\r\n\t\t\t
</tr>
<tr>
\r\n\t\t\t\t
<td>\r\n <span id=\"ContentPlaceHolder1_vaultsListGridview_fullDateLabel_1\">31/05/2017</span>\r\n </td>
<td>\r\n <span id=\"ContentPlaceHolder1_vaultsListGridview_detailLabel_1\">NİSAN AYI KOMİSYON</span>\r\n </td>
<td>\r\n 1,351.00\r\n </td>
<td>\r\n 0\r\n </td>
\r\n\t\t\t
</tr>
\r\n\t\t
</tbody>
\r\n\t
</table>
\r\n
</div>
<br/>
<div class='netWorthClass'>Giren Miktar = 1351 TL</div>
<div class='netWorthClass'>Çıkan Miktar = 1295 TL</div>
<br/>
</body>
</html>
I've taken the above html part from debugger sorry for the mess, but you see the problematic characters above.
I'll gladly try out everything you may offer. Thanks in advance.

How can i loop over an ArrayList using thymeleaf & spring Boot?

I am new to thymeleaf and i try to loop over a ArrayList but it doesn't work for me .. some help please:
this is my Html page:
<body>
<div class="row">
<table>
<tr th:each="data: mois">
<td class="text-center" th:text="${data}">data</td>
</tr>
</table>
</div>
</body>
this is My controller
#RequestMapping(value="/editePlanning", method= RequestMethod.GET)
public String editePlanning(Model model){
Psi psi = psiRepository.findOne((long) 1);
List<String> data = new ArrayList<String>();
for(int i=0;i<psi.getNombreMois();i++){
int val = psi.getMoisDebut()+i%12;
data.add(""+ val);
}
model.addAttribute("mois",data);
return "editePlanning";
}
You have a typo in your iteration (see the docs, they are very good):
<tr th:each="data: ${mois}">
Don't forget you can get the iteration index, useful to generate the id of elements
<tr th:each="data, iterstat: ${mois}">
<td th:text="${data}" th:id="|td${iterstat.index}|"></td>
</tr>

ASP.NET mvc View with IEnumerable model and input tag helper

In this official ASP.NET Core tutorial, I can use an Input Tag Helper as shown below. But due to a known model binding issue of form elements in a foreach loop, I want to use for loop instead. Question: If I were to replace #foreach (var item in Model) with #for (int i=0; i < Model.Count(); i++) in the following View. What would be my asp-for in <input asp-for="???" /> ? For some reason, intellisense is not recognizing, e.g, Model[i].BlogId or #Model[i].BlogId
#model IEnumerable<EFGetStarted.AspNetCore.NewDb.Models.Blog>
#{
ViewBag.Title = "Blogs";
}
<h2>Blogs</h2>
<p>
<a asp-controller="Blogs" asp-action="Create">Create New</a>
</p>
<table class="table">
<tr>
<th>Id</th>
<th>Url</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<input asp-for="#item.BlogId" />
</td>
<td>
<input asp-for="#item.Url" />
</td>
</tr>
}
</table>
I had the same problem and solved it this way:
#for (int i = 0; i < Model.Translations.Count; i++)
{
#Html.HiddenFor(m => m.Translations[i].LanguageId)
<form-group-text asp-for="#Model.Translations[i].Content"/>
}
The important part is using the for loop, and accessing the asp-for via #Model.
That said, you should rethink, if your Model itself should be IEnumerable. Instead create a property for it. Also you need to make sure, your Collection is accessable via Index. You could make your property of type IList. When you assign an IEnumerable to it, you can call ToList before.
#model IEnumerable<EFGetStarted.AspNetCore.NewDb.Models.Blog>
#{
ViewBag.Title = "Blogs";
}
<h2>Blogs</h2>
<p>
<a asp-controller="Blogs" asp-action="Create">Create New</a>
</p>
<table class="table">
<tr>
<th>Id</th>
<th>Url</th>
</tr>
#for (int item = 0; item < Model.Count(); item++)
{
<tr>
<td>
<input name="#Model.ToList()[item].BlogId" />
</td>
<td>
<input name="#Model.ToList()[item].Url" />
</td>
</tr>
}
</table>

Selecting a date from the calendar using selenium

I want to select any date from the calendar to test some application .
I cant find the table or tr or td elements here .
I think i can get the table divison but not able to go ahead of this .
This is my solution
public String findElementInTable(WebDriver driver,String elementXpath) {
WebElement select2Element = driver.findElement(By.xpath(elementXpath));
select2Element.click();
WebElement datepicker = driver.findElement(By.xpath("//div[#class='period_picker_days']/table"));
List<WebElement> rows_table = datepicker.findElements(By.tagName("tr"));
int rows_count = rows_table.size();
// Loop will execute till the last row of table.
for (int row = 0; row < rows_count; row++) {
// To locate columns(cells) of that specific row.
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
// To calculate no of columns(cells) In that specific row.
int columns_count = Columns_row.size();
// Loop will execute till the last cell of that specific row.
for (int column = 0; column < columns_count; column++) {
// To retrieve text from that specific cell.
if (Columns_row.get(column).getText().equals(name)) {
String celtext = Columns_row.get(column + 2).getText();
System.out.println("Cell Value Of row number " + row + " and column number " + (column) + " Is "
+ celtext);
return Columns_row.get((column + 2)).getText();
}
}
}
return null;
}
Here is a detailed html snippet of the calendar
<div class="ParkingWidgetParkingLanding period_picker_box xdsoft_noselect animation xdsoft_norange visible active periodpickerstart" style="left: 487.467px; top: 857px; width: 511px; height: 294px;" unselectable="on">
<div class="period_picker_head">
<div class="period_picker_work">
<div class="period_picker_days">
<a class="periodpicker_btn_prev" href="#">Prev</a>
<table>
<tbody>
<tr>
<td class="period_picker_month5 periodpicker_table_container">
<table>
<tbody>
<tr>
<th class="period_picker_month" title="May 2016" colspan="7" data-dateend="2016/05/31" data-datestart="2016/05/1">May 2016</th>
</tr>
<tr class="period_picker_first_letters_tr">
<th class="period_picker_holiday">Sun</th>
<th class="">Mon</th>
<th class="">Tue</th>
<th class="">Wed</th>
<th class="">Thu</th>
<th class="">Fri</th>
<th class="period_picker_holiday">Sat</th>
</tr>
<tr>
<td class="period_picker_gray_period">1</td>
<td class="period_picker_gray_period">2</td>
<td class="period_picker_gray_period">3</td>
<td class="period_picker_gray_period">4</td>
<td class="period_picker_cell period_picker_weekday" data-date="2016/05/5">5</td>
<td class="period_picker_cell period_picker_weekday" data-date="2016/05/6">6</td>
<td class="period_picker_cell period_picker_holiday period_picker_last_cell" data-date="2016/05/7">7</td>
</tr>
<tr>
<tr>
<tr>
<tr>
</tbody>
</table>
</td>
<td class="period_picker_month6 periodpicker_table_container">
<table>
Hi please do it like below (answer is as per the html source provided)
List<WebElement> dates = driver.findElements(By.xpath("//*[#class='period_picker_month5 periodpicker_table_container']/table/tbody/tr[3]/td"));
// for printing every date
// and selecting a date form the calender
String MyDate = "6";
for (int j = 0; j < dates.size(); j++) {
String date = dates.get(j).getText();
System.out.println(date);
if(dates.get(j).getText().equals(MyDate)){
dates.get(j).click();
break;
}
}
Try this for first date //html/body/div/div/div/div/table/tbody/tr/td/table/tbody/tr[3]/td[1]
Also you can download xpath finder, it's mozzila add-on i think it helps a lot.
When you install it just right click on element, choose Xiew xpath-> and it will show xpath for element.

html wont render using ng-bind-html-unsafe

i was able to break string to chars array and surround each char in <span></span> but when im trying to pass this array to table the html wont render.
breaking the string:
//parse cron_format and edit each digit individually
$scope.parse = function (cron_format){
var parsed = cron_format.split('');
for(var i = 0; i < parsed.length; i++) {
parsed[i] = '<span>' + parsed[i] + '</span>';
}
return parsed;
}
when i try to create the table like this:
<table class="table table-bordered table-hover">
<thead>
<td>user name</td>
<td>script name</td>
<td>cron format</td>
</thead>
<tbody ng-repeat="(user_id,script_id) in data | filter: test">
<tr ng-repeat="(script_id, cron_format) in script_id">
<td>{{user(user_id)}}</td>
<td>{{script(script_id)}}</td>
<td ng-bind-html-unsafe="{{parse(cron_format)}}"></td>
</tr>
</tbody>
</table>
there are no values in cron_format:
without trying to render -> <td>{{parse(cron_format)}}</td>
the table looks like this:
what am i doing wrong?
UPDATE:
i changed function's two last rows :
$scope.parsed.htmlSafe = $sce.trustAsHtml(parsed.html);
return parsed;
and i get this error:
Can't interpolate: {{parse(cron_format)}}
TypeError: Cannot set property 'htmlSafe' of undefined
can someone explain whats the mistake im doing here?