Concatenating image path from Database in asp.net core - asp.net-mvc

I am trying to concatenate a base path of image folder with a complete path coming from a DB in a .cshtml file to show image on the page but nothing is working out in my end. The best solution that I have tried is,
#foreach (var item in Model)
{
string img = #"~/pics"+Html.DisplayFor(modelItem=>item.ImagePath);
<article>
#*My Dynamic Article*#
<div class="content">
<h3>#Html.DisplayFor(modelItem => item.Name)</h3>
<img src="#img" width="95%" alt="Laser" />
<p>Hello</p>
</div>
</article>}
But this too is outputting something strange, I see this path in the output on web browser,
<img src="~/picsMicrosoft.AspNetCore.Mvc.ViewFeatures.StringHtmlContent" width="95%" alt="Laser" />
Tell me some method that works fine with asp.net core MVC latest release. Thanks

To solve this problem, you can put the content of the img variable directly into the src attribute, and add the # symbol directly in front of Html.DisplayFor(modelItem=>item.ImagePath).
Here is the work code:
#foreach (var item in Model)
{
<article>
#*My Dynamic Article*#
<div class="content">
<h3>#Html.DisplayFor(modelItem => item.Name)</h3>
<img src="~/pics/#Html.DisplayFor(model =>item.ImagePath)" width="95%" alt="Laser" />
<p>Hello</p>
</div>
</article>}
Or you can directly get ImagePath by following:
<img src="~/pics/#item.ImagePath" width="95%" alt="Laser" />
Here is the test result:

Related

create relative links with Thymeleaf

I have the following code which loops through a list of countries and creates a href links
<div class="container" th:each="country: ${countryList}">
<a th:href="${country.countryAbbr}"><div clss="row" th:text="${country.country}"></div></a>
</div>
The current page url is "localhost:8080/directory", and the generated url is showing as
"localhost:8080/us"
How can I make the url show as "localhost:8080/directory/us"
I want "us" to be added to the current url of the page.
Try create the following code in your Controller class.
#RequestMapping(value="/", method=RequestMethod.GET)
public String rootGet() {
return "redirect:/directory";
}
You can use ServletUriComponentsBuilder:
<div th:with="urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder)}"
class="container" th:each="country: ${countryList}">
<a th:href="${urlBuilder.fromCurrentRequest().path(${country.countryAbbr}).toUriString()}">
<div clss="row" th:text="${country.country}"></div>
</a>
</div>
Give a try to this one
<div class="container" th:each="country: ${countryList}">
<a th:href="#{${country.countryAbbr}}"><div clss="row" th:text="${country.country}"></div></a>
</div>
Using # usually resolves the default context but I am not aware of your environment.
For example, if you had Tomcat .war file, and your application would be hosted at localhost:8080/myApp, you would want result /myApp/us rather then /us. # would do that trick.
If above is not relevant for you, use this one:
<div class="container" th:each="country: ${countryList}">
<a th:href="#{'/directory/' + ${country.countryAbbr}}"><div clss="row" th:text="${country.country}"></div></a>
</div>

Displaying attached image with post how to i get it to display

<div>
<div class="col-md-4">
<h3 class="textStrong">Latest Tweets</h3>
<a class="twitter-timeline" href="https://twitter.com/RFUK">Tweets by RFUK </a></div>
</div>
<div class="col-md-4"></div>
<div class="col-md-4">
<h2>News Feeds</h2>
#{
var news = new List<Piranha.Entities.Post>();
using (var db = new Piranha.DataContext()) {
news = db.Posts
.Include(p => p.CreatedBy)
.Where(p => p.Template.Name == "News Post Types")
.OrderByDescending(p => p.Published)
.Take(4).ToList();
}
}
#foreach (var post in news) {
<div class="post">
<h2>#post.Title</h2>
<p class="meta">Published #post.Published.Value.ToString("yyyy-MM-dd") by #post.CreatedBy.Firstname</p>
<p>#post.Excerpt</p>
<img src="#post.Attachments">
</div>
I working with posts. I have this code to work with.... Works really well I might add.. However the attached image I wish to display with the post. How can I do that?
<img src="#post.Attachments">
It doesn't appear to work any suggestions
on how I sort what I need to do?
Like #andreasnico pointed out Attachments is a collection of referenced media asset id's. If you want to display the first attachment (assuming you know it's an image) you'd probably do like this.
#foreach (var post in news) {
<div class="post">
<h2>#post.Title</h2>
<p class="meta">Published #post.Published.Value.ToString("yyyy-MM-dd") by #post.CreatedBy.Firstname</p>
<p>#post.Excerpt</p>
#if (post.Attachments.Count > 0) {
<img src="#UI.Content(post.Attachments[0])">
}
</div>
}
This would get the content URL for the first attachment and use it as the source to the image. Note that you can also scale & crop images for use in lists like this with:
<img src="#UI.Content(post.Attachments[0], 300, 100)">
This would scale & crop the image to be 300px wide & 100px high. You can read more about this here: http://piranhacms.org/docs/api-reference/ui-helper
Also if the page displaying the post list is controlled by the CMS and has a page type I'd suggest you look into adding either a PostRegion or PostModelRegion to that page. These region automatically loads a collection of post into the page model, you can specify the amount, sort order & some other stuff. This will simplify you reusing the page type but for example changing which type of post to display for different page instances.
Regards
Håkan

MODX Articles: Can't retrieve article image with getResources

I am running MODX Revo 2.3.2 with Articles and getResources. On my start page, I wan't to show the 4 most recent articles by calling getResources. However, it does not show the image. Tried to output it without phpThumbof, but it does not work out at all.
[[getResources?
&parents=`11`
&tpl=`articleRowTpl`
&includeTVs=`1`
&processTVs=`1`
&showHidden=`1`
&limit=`4`
]]
My articleRowTpl looks like this:
<article class="blog-item blog-full-width">
<div class="blog-thumbnail">
<img alt="" src="[[+tv.articleImage:phpthumbof=`w=750&h=200&zc=1`]]">
</div>
<div class="blog-full-width-date">
<p class="day">[[+publishedon:strtotime:date=`%d`]]</p><p class="monthyear">[[+publishedon:strtotime:date=`%b %Y`]]</p>
</div>
<div class="blog-content">
<h4 class="blog-title">[[+pagetitle]]</h4>
<p>[[+introtext:default=`[[+content:ellipsis=`400`]]`]]</p>
Mehr lesen...
</div>
</article>
There are no errors, it just does not output the image path. I double checked the placeholder and the prefix, which defaults to tv. as the documentation says.
Alright, found it. I just removed the TV-Prefix of getResources with
tvPrefix=``
and now I can access the TV with [[+articleImage]].

How to make IDs of components (Div, a, h1, etc) of a webpage Dynamic (variable?) using MVC2?

I am trying to do the following:
The object I give to the Viewpage has a list, I do a foreach in the HTML and I create a number of components. Now, I want the IDs of those components to somehow be linked to the object from the List.
(The reason I am trying to do this is because I want to show a button, when they press that button the shown content will change and they should see something else, I will use javascript to achieve this)
Therefor I am trying to make the id of those components dynamic, by for example stating
id="button <%= item.id%>" however this does not seem to work. I have searched alot on google but I haven't found a solution yet which is why I turn to you guys.
I'll link my code as well, I deleted some of the parts that were unnecessary (but added the javascript):
<script type="text/javascript">
function AlterPanel(thePanel) {
var panel = document.getElementById("region"+thePanel);
panel.style.display = 'block';
var button = document.getElementById("button"+thePanel);
button.style.display = 'none';}
</script>
<%foreach (TeamDTO team in Model.List.Teams)
{ %>
<a id="button<%= team.Number %>" onclick="AlterPanel(<% team.Number%>)">
Add member</a>
<div Visible="false" id='region<%= team.Number %>' runat="server">
Please select one:
<%: Html.DropDownListFor(V => V.memberID, new SelectList(Model.members, "ID","Name")) %>
</div>
<% } %>
I eagerly await a reply and thank you in advance.
I guess your Queastion is:
-you have some "button DropDownList" pair, button is visiable, DropDownList is invisible, now if user click the button then DropDownList will showup.
OK, now your View maybe :
<%foreach (TeamDTO team in Model.List.Teams)
{ %>
<a onclick="AlterPanel(<% team.Number%>)">
Add member</a>
<div id="region<%= team.Number %>" style="display:none">
Please select one:
<%: Html.DropDownListFor(V => V.memberID, new SelectList(Model.members, "ID","Name")) %>
</div>
<% } %>
I use JQuery in javascript part like this:
<script type="text/javascript">
function AlterPanel(thePanel) {
$("#region" + thePanel.toString()).css("display", "block");
}
</script>
Don't forget include the following file in View():
<script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery-1.4.1.min.js") %>"></script>
if the answer is not what you want, let mt know and I can help you~ :)

MVC - RouteLink and Image

I'd like this output:
<a href="\Catalog\Flooring">
<img src="http://site.com/dot.jpg" width="100px" height="100px" alt="" />
<span>Some text here</span>
</a>
using a RouteLink similar to:
<%= Html.RouteLink(myFPV.ProductTypeName, "CatalogType", new { controller = "Catalog", action = "Types", group = myFPV.ProductGroupName, type = myFPV.ProductTypeName })%>
I cannot figure out how to add an <img> and <span> (with text) tags inside my <a> tag.
Make sense?
The first parameter of the RouteLink method is for the link text. But unfortunately, it gets encoded automatically, so you cannot send <, > characters to it. (Well, you can. But they'd get encoded.)
Take a look at this page.
Route values are URL encoded automatically. For example, the string “Hello World!” is
encoded to “Hello%20World!”.
Rendering Image Links
Unfortunately, you can’t use the
Html.ActionLink() helper to render an
image link. Because the
Html.ActionLink() helper HTML encodes
its link text automatically, you
cannot pass an tag to this
method and expect the tag to render as
an image.
Instead, you need to use the
Url.Action() helper to generate the
proper link. Here’s how you can
generate a delete link with an image:
<a href="<%= Url.Action("Delete") %>">
<img src="../../Content/Delete.png" alt="Delete" style="border:0px" />
</a>
I suggest use the Url.RouteUrl.
<a href="#Url.RouteUrl("Default", new { action = "Index", controller = "Department", id = 1 })">
<img src="http://site.com/dot.jpg" width="100px" height="100px" alt="" /> </a>
There is a better option to do that. you should try some thing like
<a href="#Url.RouteUrl("index-lang")">
<img src="~/images/logo.png" alt="">
</a>
where the "index-lang" is the route name in route mapping table.

Resources