asp.net mvc - Print inner text of a HtmlContent in a razor view (MVC4) -
using mvc, @ example have following html code:
<p>duma</p><img url='..' /><p>duma</p>
i print content of tags, as: duma duma
, removing image, tags , showing text (as innertext)
i tried using html.raw() it's not worked. reading class tabbuilder
, creating html extension method, don't have idea how implemeted in razor view.
you make string extension strip html tags.
public static class stringextensions { public static string striphtml (this string inputstring) { return regex.replace (inputstring, "<.*?>", string.empty); } }
then use in view
@myhtmlstring.striphtml()
you might need declaring using statement stringextensions class or add web.config file in views folder
@using my.extensions.namespace
or
<system.web> <pages> <namespaces> <add namespace="my.extensions.namespace" /> </namespaces> </pages> </system.web>
you make html helper extension
public static class htmlextensions { public static string striphtml (this system.web.mvc.htmlhelper helper, string htmlstring) { return regex.replace (htmlstring, "<.*?>", string.empty); } }
you can use in view this
@html.striphtml(myhtmlstring)
you still need add reference namespace extension methods above. either adding web.config in views folder or adding using statement in view. differences here adding web.config file able use extension method in views without adding using statement.
Comments
Post a Comment