c# - How do you sanitize and publish HTML into ASP.Net View? -
my goal
save , post html blog post using c# asp.net mvc, using best practices
how do this?
below source causes following error:
error 2 operator '.' cannot applied operand of type 'lambda expression'
articlecontent.cs (controller)
// post: article/articles/create [httppost] [validateantiforgerytoken] [authorize(roles = "canedit")] public actionresult create([bind(include = "postid,title,publishdate,createdate,modifydate,author,pagebody,feature,published,excerpt,createdateutc,publishdateutc,modifydateutc,canonicalurl,urlslug,category,tag")] articlecontent articles) { if (modelstate.isvalid) { db.articles.add(articles); db.savechanges(); return redirecttoaction("index"); } return view(articles); }
article.cs (model)
[display(name = "body of article")] public string pagebody { get; set; }
index.cshtml (view) <-- trigger
@html.raw(httputility.htmldecode((modelitem => modelitem.pagebody).tostring().substring(0,250)))
index.cshtml (full version of view)
<section class="bg-ocean row light text-center shelve"> @if (user.isinrole("cancreate")) { <button class="btn btn-info btn-lg btn-block"> @html.actionlink("create new", "create") </button> } @foreach (var item in model) { <article class="headline"> <div class="col-xs-12 col-md-7 headline-image np nm"> <a href="@url.action("details", "details", new { id = item.postid })" target="_blank"> <img src="@html.displayfor(modelitem => item.feature)" /> <span> @html.displayfor(modelitem => item.categories) </span> </a> </div> <div class="col-xs-12 col-md-5 bg-light headline-content np nm"> <a href="@url.action("details", "details", new { id = item.postid })" target="_blank"> <small class="dark">@html.displayfor(modelitem => item.modifydate)</small> <h2 class="media-heading dark">@html.displayfor(modelitem => item.title)</h2> <hr /> <p>@html.displayfor(modelitem => item.excerpt)</p> </a> <div class="headline-footer"> <div class="btn-group btn-group-justified"> <span class="btn btn-default" role="button"><i class="fa fa-eye"></i> 172</span> <span class="btn btn-default" role="button"><i class="fa fa-comment"></i><a href="@url.action("details", "details", new { id = item.postid + "%23disqus_thread" })"></a></span> <span class="btn btn-default highlight" role="button"><i class="fa fa-share"></i> 210</span> </div> </div> </div> @if (user.isinrole("canedit")) { @html.actionlink("e", "edit", new { id = item.postid }) } @if (user.isinrole("candelete")) { @html.actionlink("d", "delete", new { id = item.postid }) } </article> } </section>
questions resources
how save html database , retrieve properly
future note
using .substring(0,250)
null value creates error due strings 0 - 250 not existing.
the httputility.htmldecode
takes string , returns another. pass lambda expression (modelitem => modelitem.pagebody
) on tostring
- , substring
methods called.
you must change code following:
@html.raw(httputility.htmldecode(model.pagebody).substring(0,250))
in code, give pagebody
property htmldecode
method , call substring
. string given raw
method.
much methods @html.editorfor
take lambda argument, htmldecode
needs string. main problem.
ps: if pagebody
is not string, must call tostring
on before passing htmldecode
.
Comments
Post a Comment