开发者

Response.write in a HTML element c#

开发者 https://www.devze.com 2022-12-11 02:21 出处:网络
I am picking my way through some c# code I inherited, and trying to show an image within a link;this is the line that works, and show\'s the image, but I need to wrap this in a link.

I am picking my way through some c# code I inherited, and trying to show an image within a link;this is the line that works, and show's the image, but I need to wrap this in a link.

Response.Write(Html.PropImage(item.MainImage.ImageUrl095, item.AccomName));

I noticed later on there is a link element, so I thought I could copy this (as it is the right link I need) and wrap it around the HTML.PropImage line.Like this

Html.PropRefLink(item.AreaName, item.AccomName, item.AccomCode, Html.PropImage(item.MainImage.ImageUrl095, 开发者_运维技巧item.AccomName), (string)ViewData["PDate"], (string)ViewData["PDuration"], (string)ViewData["PSleeps"]); 

I removed the response.write as it was breaking the page, how can I that html.propimage to render within the link ?

Thanks


Since the function only allows link text (or so it seems from your comment) your best bet would be to use flat HTML. Change the link text to:

"<img src=\"" + item.MainImage.ImageUrl095 + "\" alt=\"\" />"

This will render a standard HTML image tag within your link and should work as expected.


I would suggest that you are best off creating your own Html Helper Extension Method to create an img element wrapped within the link element. you can use the StringBuilder class for this

HTMl Helper Tutorial

Basic Example:

using System;
using System.Web.Mvc;
using System.Text;

namespace MvcApplication1.Helpers
{
    public static class ImageHelpers
    {
        public static string ImageLink(this HtmlHelper helper, string imagepath, string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<a href=\"" + url + "\">");
            sb.Append("<img src=\"" + imagepath + "\" />");
            sb.Append("</a>");
            return sb.ToString();
        }
    }
}

Then you would call your extension method within your view as follows:

<%= Html.ImageLink("imagepath","theurl") %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号