I've just written an MVC helper method to write img
tags for me, in the method I'm hardcoding a nopic
image that should be used when no image path is passed in. I don't like the hardcoding but don't know what the best approach I could use (given this is MVC and this is a helper) to achieve it would be.
public static MvcHtmlString Image(this HtmlHelper htmlHelper,
string imgName, string alt, int height)
{
if (string.IsNullOrWhiteSpace(imgName)) imgName = "nopic.jpg";
var src = String.Format("/Content/ProductImages/{0}", imgName);
var tagBuilder = new TagBuilder("img");
tagBuilder.Attributes.Add("src", htmlHelper.Encode(src));
tagBuilder.Attributes.Add("alt", htmlHelper.Encode(alt));
tagBuilder.Attributes.Add("height", htmlHelper.Encode(height));
return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
}