मैं लुकास से जवाब शामिल हो गए हैं और "ASP.NET MVC Helpers, Merging two object htmlAttributes together" ए nd प्लस निम्नलिखित कोड को controllerName:
CSHTML
@Html.ActionImage("Edit",
"EditController"
new { id = MyId },
"~/Content/Images/Image.bmp",
new { width=108, height=129, alt="Edit" })
में // नमूना उपयोग और इसके बाद के संस्करण कोड के लिए विस्तार वर्ग:
using System.Collections.Generic;
using System.Reflection;
using System.Web.Mvc;
namespace MVC.Extensions
{
public static class MvcHtmlStringExt
{
// Extension method
public static MvcHtmlString ActionImage(
this HtmlHelper html,
string action,
string controllerName,
object routeValues,
string imagePath,
object htmlAttributes)
{
//https://stackoverflow.com/questions/4896439/action-image-mvc3-razor
var url = new UrlHelper(html.ViewContext.RequestContext);
// build the <img> tag
var imgBuilder = new TagBuilder("img");
imgBuilder.MergeAttribute("src", url.Content(imagePath));
var dictAttributes = htmlAttributes.ToDictionary();
if (dictAttributes != null)
{
foreach (var attribute in dictAttributes)
{
imgBuilder.MergeAttribute(attribute.Key, attribute.Value.ToString(), true);
}
}
string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);
// build the <a> tag
var anchorBuilder = new TagBuilder("a");
anchorBuilder.MergeAttribute("href", url.Action(action, controllerName, routeValues));
anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);
return MvcHtmlString.Create(anchorHtml);
}
public static IDictionary<string, object> ToDictionary(this object data)
{
//https://stackoverflow.com/questions/6038255/asp-net-mvc-helpers-merging-two-object-htmlattributes-together
if (data == null) return null; // Or throw an ArgumentNullException if you want
BindingFlags publicAttributes = BindingFlags.Public | BindingFlags.Instance;
Dictionary<string, object> dictionary = new Dictionary<string, object>();
foreach (PropertyInfo property in
data.GetType().GetProperties(publicAttributes))
{
if (property.CanRead)
{
dictionary.Add(property.Name, property.GetValue(data, null));
}
}
return dictionary;
}
}
}
सीधे संबंधित नहीं है, लेकिन मैं दृढ़ता से सुझाव देता हूं कि आप पीएनजी या जेपीजी फाइलों (छवि सामग्री के आधार पर) का उपयोग करें o एफ बीएमपी फाइलें। और @jgauffin की तरह सुझाव दिया गया है, एप्लिकेशन रिश्तेदार पथ ('~/Content') का उपयोग करने का भी प्रयास करें। पथ '../../ सामग्री' अलग-अलग मार्गों से मान्य नहीं हो सकता है (उदा।'/','/होम', '/ होम/इंडेक्स')। – Lucas
धन्यवाद लुकास। मैं पीएनजी का उपयोग करता हूं लेकिन यूआरएल का उपयोग करने के लिए सलाह। सामग्री वह है जिसे मैं ढूंढ रहा था। वोट दें :) – davy