mvc show no picture if can't find file - asp.net-mvc

I have a mvc project. there is a column like this
<td>
<img width="60" src='#Url.Content("~/Content/photos/" + #item.ID+".jpg")' alt="Image" />
</td>
But if no jpg file exists, I wanna show another general picture. How could I do that? Thanks

I am presuming that your item ID is coming from your model. If this is the case you could use Razor to solve this problem. This is one solution:
#if (item.ID != null)
{
<td>
<img width="60" src='#Url.Content("~/Content/photos/" + #item.ID+".jpg")' alt="Image" />
</td>
}
else
{
//code to show general picture here
}

I figure it could use it like this
#if (System.IO.File.Exists(Server.MapPath("~/Content/photos/" + #item.ID + ".jpg")))
{
<img width="60" src='#Url.Content("~/Content/photos/" + #item.ID+".jpg")' alt="Image" />
}
else
{
<img width="60" src='#Url.Content("~/Content/photos/noPic.jpg")' alt="Image" />
}

Related

Thymeleaf, how to make conditional statement with th:src?

If dish.imageFolder is not null - it should be
<img th:src="#{${dish.imageFolder}}"/>
If null - this image
<img src="https://via.placeholder.com/150" alt=""/>
One option:
<img th:src="${dish.imageFolder != null} ? #{${dish.imageFolder}} : 'https://via.placeholder.com/150'" />
I would probably just use th:if though, like this:
<img th:if="${dish.imageFolder != null}" th:src="#{${dish.imageFolder}}" />
<img th:if="${dish.imageFolder == null}" src="https://via.placeholder.com/150" />

Dynamic column in ASP.NET MVC 5 [duplicate]

I am writing some very poor code right now and before I even save it I was hoping to get some input on improving it. I am trying to build an html table with three cells to every row. If the collection has 5 items then that should render as two rows.
The code I have written so far I can see is not very robust and will require constant maintenance but I am unsure of other tools / methods for accomplishing the task.
<table>
#foreach (VideosModel item in Model)
{
if (cellCount == 0 || cellCount == 3)
{
#Html.Raw("<tr>")
}
<td style="padding:0px 20px;">
#item.VideoTitle <br />
<a href="#item.VideoUrl" target="_blank">
<img src="../../.../Video.jpg" /> <br />
</a>
#item.VideoDescription
<br />
</td>
cellCount++;
if (cellCount == 3 || cellCount == 6)
{
#Html.Raw("</tr>")
}
if (cellCount > 3) { cellCount = 0; }
}
</table>
You should considering using modulo instead of comparing the values of cellCount to 0, 3 or 6:
<table>
#foreach (VideosModel item in Model)
{
if (cellCount % 3 == 0)
{
#:<tr>
}
<td style="padding:0px 20px;">
#item.VideoTitle <br />
<a href="#item.VideoUrl" target="_blank">
<img src="../../.../Video.jpg" />
</a><br />
#item.VideoDescription
<br />
</td>
cellCount++;
if (cellCount % 3 == 0)
{
#:</tr>
}
}
</table>

if condition in the razor syntax for HTML table- MVC

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")" />
}

Dynamically generate table in loop / foreach MVC view

I am writing some very poor code right now and before I even save it I was hoping to get some input on improving it. I am trying to build an html table with three cells to every row. If the collection has 5 items then that should render as two rows.
The code I have written so far I can see is not very robust and will require constant maintenance but I am unsure of other tools / methods for accomplishing the task.
<table>
#foreach (VideosModel item in Model)
{
if (cellCount == 0 || cellCount == 3)
{
#Html.Raw("<tr>")
}
<td style="padding:0px 20px;">
#item.VideoTitle <br />
<a href="#item.VideoUrl" target="_blank">
<img src="../../.../Video.jpg" /> <br />
</a>
#item.VideoDescription
<br />
</td>
cellCount++;
if (cellCount == 3 || cellCount == 6)
{
#Html.Raw("</tr>")
}
if (cellCount > 3) { cellCount = 0; }
}
</table>
You should considering using modulo instead of comparing the values of cellCount to 0, 3 or 6:
<table>
#foreach (VideosModel item in Model)
{
if (cellCount % 3 == 0)
{
#:<tr>
}
<td style="padding:0px 20px;">
#item.VideoTitle <br />
<a href="#item.VideoUrl" target="_blank">
<img src="../../.../Video.jpg" />
</a><br />
#item.VideoDescription
<br />
</td>
cellCount++;
if (cellCount % 3 == 0)
{
#:</tr>
}
}
</table>

How to add model value to the html attribute value

I have this code
#if (ViewBag.TechnologyNames != null)
{
foreach (var technologyName in ViewBag.TechnologyNames)
{
if (#technologyName.TechnologyID == -1)
{
<div class="CheckBoxItem">
<input type="checkbox" name="option1" id="allTechnology" value="#technologyName.TechnologyID" checked="checked" />
#technologyName.Name
</div>
}
else
{
<input type="checkbox" name="option2" id="tech" value="#technologyName.TechnologyID" />
#technologyName.Name
}
}
}
What I need is somehow to add #technologyName.TechnologyID to id="tech" to have at the end
id="tech_123"
How I can do it?
Thank you!
try this (basically adding parenthesis around the property):
<input type="checkbox" name="option2" id="tech_
#(technologyName.TechnologyID)" value="#technologyName.TechnologyID" />

Resources