我会写一个HTML帮手:
public static class HtmlExtensions
{
public static MvcHtmlString StudentImage(this HtmlHelper<StudentViewModel> htmlHelper)
{
var model = htmlHelper.ViewData.Model;
var relativeImagePath = "~/uploads/foto_" + model.StudentID + ".jpg";
var imagePath = htmlHelper.ViewContext.HttpContext.Server.MapPath(relativeImagePath);
var image = new TagBuilder("img");
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
image.Attributes["src"] = urlHelper.Content("~/uploads/some_generic_image.jpg");
image.Attributes["alt"] = model.FullName;
image.Attributes["style"] = "height: 125px; width: 125px";
if (File.Exists(imagePath))
{
image.Attributes["src"] = urlHelper.Content(relativeImagePath);
}
return MvcHtmlString.Create(image.ToString());
}
}
,然后在你看来简单:
<%= Html.StudentImage() %>
如果您有任何疑问,随时问更多。 – lock 2011-06-26 06:31:17