I have a tag and I'm using Razor syntax. Im using the @url.Action html helper to pass a parameter to a method on a controller to retrieve the picture. But I want to have this picture as a link so when the user click the picture it would go to the controller and open a different view. So I tried the both ways show below but it's telling me that it's missing a "}" for "Tryone". And for "Trytwo" its not giving me any errors but it does not show me the picture as a link. Any ideas of the wright way to do this?
tryone
@foreach (var p in Model)
{
<a href= "@Url.Action("Index","Home")>
<img width="50" height="50"
src= "@Url.Action("GetImage", "Sells", new {p.ItemID })" />
</a>
}
trytwo
@foreach (var p in Model)
{
<img href="@Url.Action("Index","Home")" width="50" height="50"
src= "@Url.Action("GetImage", "Sells", new {p.ItemID })" />
}
An issue with your first attempt is that the href
attribute is missing a closing quotation mark before the ending angle brace.
@foreach (var p in Model)
{
<a href= "@Url.Action("Index","Home")">
<img width="50" height="50"
src= "@Url.Action("GetImage", "Sells", new {p.ItemID })" />
</a>
}
trytwo is not working because img
does not support href
attribute.
Use your first approach with correct syntax - add quote (") at the end of href
value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With