asp.net mvc - MVC Html Helper not rendering image within a link -
I am creating a custom assistant to create an image link as follows:
Public stable MvcHtmlString ImageLink (This HtmlHelper Assistant, String Image URL, String Image ALT, String Link URL, String Linkite, String Link Target) {// Image Object Var IMG = New Tag Builder ("IMG"); // img.MergeAttribute ("src", imageUrl) to add your attributes; Img.MergeAttribute ("alt", imageAlt); // link var link = create new TagBuilder ("a"); // link.MergeAttribute ("href", linkUrl) to add your attributes; Link.MergeAttribute ("title", linkTitle); Link.MergeAttribute ("Target", linkTarget); // Set the internal HTML of the links of the IMG link. INHML = IMG. ToString (); // return the link tag at the end Return to MvcHtmlString.Create (link.ToString (TagRenderMode.EndTag)); } But, when I use it, it does not render anything. When I change the previous line to the following:
Back MvcHtmlString.Create (link.ToString (TagRenderMode.SelfClosing)); It only renders a tag and captures the text beyond the statement, for example:
Hello @html Image link ("... parameter") world The result here is that the 'world' text is wrapped in anchor, but there is no image. I did not even know it This can happen, because the word 'world' is not part of the helpful narrative.
I finally changed the final description to: Back MvcHtmlString.Create (link.ToString (TagRenderMode.Normal)); This works, but why is my question? I thought EndTag has given more importance, especially when looking at the details given by Intelligence for that choice.
The last 2 lines of your code should be:
link InnerHtml = IMG ToString (TagRenderMode.SelfClosing); Return MvcHtmlString.Create (link.ToString ()); You do not need TagRenderMode. Normally this is the default mode. To use it in your view, you want to do this:
@ Html.ImageLink ("/ images / test.jpg", "testalt", "http: // testlink" , "I thought EndTag made more sense You can think of this, but TagRenderMode.EndTag presents only the closing tag TagRenderMode .Normal is what makes you the normal HTML tags and allows InnerHtml .
Comments
Post a Comment