Guest User

Untitled

a guest
Dec 9th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. using System.Web;
  2. using System.Web.Mvc;
  3. using System.Web.Mvc.Html;
  4.  
  5. namespace My.Helpers
  6. {
  7. /// <summary>
  8. /// MVC HtmlHelper extension methods - html element extensions
  9. /// </summary>
  10. public static class PartialExtensions
  11. {
  12. /// <summary>
  13. /// Allows a partial to be rendered within quotation marks.
  14. /// I use this with jQuery tooltips where we store the tooltip HMTL within a partial.
  15. /// See example usage below:
  16. /// <div class="tooltip" title="@Html.PartialAttributeEncoded("_MyTooltipInAPartial")">Some content</div>
  17. /// </summary>
  18. /// <param name="helper"></param>
  19. /// <param name="partialViewName"></param>
  20. /// <param name="model"></param>
  21. /// <returns></returns>
  22. public static MvcHtmlString PartialAttributeEncoded(
  23. this HtmlHelper helper,
  24. string partialViewName,
  25. object model = null
  26. )
  27. {
  28. //Create partial using the relevant overload (only implemented ones I used)
  29. var partialString = (model == null)
  30. ? helper.Partial(partialViewName)
  31. : helper.Partial(partialViewName, model);
  32.  
  33. //Attribute encode the partial string - note that we have to .ToString() this to get back from an MvcHtmlString
  34. var partialStringAttributeEncoded = HttpUtility.HtmlAttributeEncode(partialString.ToString());
  35.  
  36. //Turn this back into an MvcHtmlString
  37. var partialMvcStringAttributeEncoded = MvcHtmlString.Create(partialStringAttributeEncoded);
  38.  
  39. return partialMvcStringAttributeEncoded;
  40. }
  41. }
  42. }
Add Comment
Please, Sign In to add comment