I am printing the data from ViewModel into DataTable in the following way, but how to obtain a space between #item.FirstName and #item.LastName so that the output can be of the form FirstName LastName? At present, I am getting the Name as FirstNameLastName. Ex: MarkStevens. I want to print it as Mark Stevens.
#if(Model.Count() != 0)
{
<thead>
<tr>
<th data-column-id="Name">Name</th>
...
</tr>
</thead>
}
else
{
<h3 style="text-align:center">No Records Found</h3>
}
<tbody>
#foreach(var item in Model)
{
<tr>
<td>
#(if (item.UserId != null)
{
#item.FirstName #item.LastName
}
else
{
#item.OtherUser
}
</td>
</tr>
}
</tbody>
You can use #Html.Raw helper which means, you render whatever inside without any html encoding like space or string:
#item.FirstName #Html.Raw(" ") #item.LastName
If you want html, you can also use a simple non-breaking spaces:
#item.FirstName #item.LastName
How about string format the output: $"{#item.FirstName} {#item.LastName}"
use string.Format. i feel this is very clean approach.
string.Format("{0} {1}", #item.FirstName,#item.LastName)
Related
I am trying to use a if statement to display records in a cell where the field DocumentType = "N/A" and then again in a different cell where the field DocumentType <> "N/A". How can this be done. I can not seem to get it correct. Here is what I am trying.
#foreach (Document documents in Model.Documents)
{
if (#documents.DocumentType == "N/A")
{
<tr>
<td class="auto-style1" style="width: 1px">#documents.Description</td>
</tr>
}
}
#foreach (Document documents in Model.Documents)
{
if (#documents.DocumentType != "N/A")
{
<tr>
<td class="auto-style1" style="width: 1px">#documents.Description</td>
</tr>
}
}
To me the approach looks ok. But it sounds like it will be better to use linq.
The list can be filtered on the foreach statement:
#foreach (Document documents in Model.Documents.Where(d => d.DocumentType == "N/A"))
{
<tr>
<td class="auto-style1" style="width: 1px">#documents.Description</td>
</tr>
}
#foreach (Document documents in Model.Documents.Where(d => d.DocumentType != "N/A"))
{
<tr>
<td class="auto-style1" style="width: 1px">#documents.Description</td>
</tr>
}
I have a HTML table with 2 rows and 4 columns. The data in each cell is coming from the stored proc. Currently, the last column displays number, I have an image to display, but it has to be displayed only if(rejected_question>0) else it should not display anything. How do I accomplish this in razor.
I have tried this so far, the below code doesn't work:
#foreach (var item in Model)
{
//logic for first three columns..
//fourth column here.
<td align="center">
if(#Html.DisplayFor(modelItem => item.RejectedQuestion) >0)
{
return Html.Raw(string.Format("<text><img height='3' width='3' src="\{0}\" alt="\Image\" /></text>", Url.Content("../../content/images/icon_red_questions_returnedbyreviewer.gif")));
}
</td>
}
This is what your looking for, probably:
#foreach (var item in Model)
{
//logic for first three columns..
//fourth column here.
<td align="center">
#if(item.RejectedQuestion > 0)
{
<img height="3" width="3" alt="Image"
src="#Url.Content("~/content/images/icon_red_questions_returnedbyreviewer.gif")" />
}
</td>
}
Make the line inside the if statement
#Html.Raw(string.Format("<text><img height='3' width='3' src="\{0}\" alt="\Image\" /></text>", Url.Content("../../content/images/icon_red_questions_returnedbyreviewer.gif")))
to display it. view.Execute() returns void, so you cannot include a return statement.
I tried this and it worked, just in case if someone needs it in future
:
#if(item.RejectedQuestion > 0)
{
<img height="20" width="20" alt="Image"
src="#Url.Content("~/content/images/icon_red_questions_returnedbyreviewer.gif")" />
}
I am populating my datatable using Linq.
I have hard-coded headers. And populating body columns with Linq. Following is my code.
<table id="tableID">
<thead>
<tr>
<th>Name</th>
<th>ID</th>
<th style="width:2%;"></th>
</tr>
</thead>
<tbody>
#if(Model.Values !=null)
{
foreach(var value in Model.Values)
{
<tr>
<td>#value.Name</td>
<td>#value.ID</td>
</tr>
}
}
</tbody>
</table>
What I am thinking to do here, if there is no data table should not be visible.I thought of moving my conditional check if model is returning null prior to table creation but it will throw exception. I am fairly new to MVC. Any suggestion is appreciated.
Thank you
Simply put one if around table as well to check if property is not null and count of that list is greater than 0 then table should be rendered.
#if(Model != null)
{
if(Model.Values != null && Model.Values.Count != 0)
{
<table id="tableID">
<thead>
<tr>
<th>Name</th>
<th>ID</th>
<th style="width:2%;"></th>
</tr>
</thead>
<tbody>
#foreach(var value in Model.Values)
{
<tr>
<td>#value.Name</td>
<td>#value.ID</td>
</tr>
}
</tbody>
</table>
}
}
I have created an e-commerce website and am using a label to display the products from my sql database, the image it shows of these products are not hyperlinks, but this is what I need them to be, I think I have written the right code but I have a "parentControl" error, could someone help plz?
Below is also a link to show you visually what is being asked. REMEMBER the picture is just an image, but needs to be a hyperlink!
private void FillPage()
{
ArrayList teesList = new ArrayList();
if (!IsPostBack)
{
teesList = ConnectionClass.GetTeesBySize("%");
}
else
{
teesList = ConnectionClass.GetTeesBySize(DropDownList1.SelectedValue);
}
StringBuilder sb = new StringBuilder();
HyperLink link = new HyperLink();
link.NavigateUrl = "http://google.com";
parentControl.Controls.Add(link);
foreach (Tees tees in teesList)
{
sb.Append(string.Format(#"<table class='TeesTable'>
<tr>
<th rowspan='1' width='150px'><img runat='server' src='{6}' /</th>
<th width='50px'>Name: </th>
<td>{0}</td>
</tr>
<tr>
<th>Size:</th>
<td>{1}</td>
</tr>
<tr>
<th>Price:</th>
<td>{2}</td>
</tr>
</table>",
tees.name, tees.size, tees.price, tees.id, tees.id, tees.id, tees.image));
LblOutput.Text = sb.ToString();
(https://dl-web.dropbox.com/get/CompetitiveStreakTemplate/Pic.png?_subject_uid=9403629&w=AAD63dzqPQcNMNSU0OwbVBrGjNGFvtt7VWJ6DKwlu4UoPw).
You need to close your <img> tag properly and wrap it in a <a> to make it a hyperlink. Also use <td> instead of <th> for data rows and not headers.
Something a bit more like:
<td rowspan='1' width='150px'>
<a href='{0}.aspx'>
<img runat='server' src='{6}' />
</a>
</td>
You should probably also tidy up the rest of the code appended to your table, checking what is a header or data, and using a <tbody> tag inside the <table>
Edit: looking closer it seems as though you aren't creating the table properly in the first place, change this chunk of your code to do the loop properly:
parentControl.Controls.Add(link);
sb.Append("<table class='TeesTable'><tbody>");
foreach (Tees tees in teesList)
{
sb.Append(string.Format(#"
<tr>
<td rowspan='1' width='150px'>
<a href='{0}.aspx'>
<img runat='server' src='{6}' />
</a>
</td>
<td width='50px'>Name: </td>
<td>{0}</td>
</tr>
<tr>
<td>Size:</td>
<td colspan='2'>{1}</td>
</tr>
<tr>
<td>Price:</td>
<td colspan='2'>{2}</td>
</tr>",
tees.name, tees.size, tees.price, tees.id, tees.id, tees.id, tees.image));
}
sb.Append("</tbody></table>");
LblOutput.Text = sb.ToString();
Well, easily I have a model:
#model IEnumerable<CompanyWebSite.Category>
And it's quite straightforward to iterating through its items and create a One-item-per-row table to show them.
And the problem is that I want to create a table like this:
<table>
<tr>
<td></td>
<td></td>
</tr>
//
// ...
//
<tr>
<td colspan="2"> ... LastItem ...</td>
</tr>
</table>
Seems simple, but I can't do that...!
#{
ViewBag.Title = "Products";
var last = Model.Last();
int i = 1;
}
<table id="tblCats">
#foreach(var item in Model)
{
if (i == 1){
<tr>
#(i = 2)
}
#if (!item.Equals(last))
{
<td>
<h4>#item.CategoryName</h4>
<label>#item.Description</label>
</td>
if (i == 2)
{
</tr>
i = 1;
}
else
i = 2;
}
else
{
<td colspan="2">
</td>
</tr>
}
}
Because of the first opening <tr>, Razor gets confused and doesn't see the closing }s and else statements... What can I do...?!
You could use HtmlHelper.Raw method to help razor