0

I am having a problem with razor and models.

In my view I have a model with a list with "article" objects.

So I do a foreach with @MvcHtmlString.Create(article.Intro) which works great.

Then when I want a substring of that intro:

@MvcHtmlString.Create(article.Intro).ToHtmlString().Substring(0, 50) the page ends in endless loop (which happens often when razor can't render something) without an error.

Does anyone know how I can get this substring ?

2
  • 1
    Why are you using MvcHtmlString.Create (instead of just @article.Intro)? Is your Intro field already HTML encoded? If not, you're causing an XSS vulnerability. If it is, then you can't just use Substring because it might cut in the middle of entity like &. Commented Mar 23, 2011 at 12:15
  • @article.intro is just plain nvarchar from db, I did the mvchtmlstring because I tought it might solve my substring problem, because I am not working directly on the property, but on the locally created htmlstring Commented Mar 23, 2011 at 12:25

2 Answers 2

5

Substring(0, 50) throws an ArgumentOutOfRangeException exception if any of your article intros is shorter than 50 characters. That's probably the cause of your strange problem.

Furthermore, the use of MvcHtmlString is incorrect since your data obviously isn't HTML encoded yet.

So a solution could be:

@article.Intro.Substring(0, Math.Min(article.Intro.Length, 50))
Sign up to request clarification or add additional context in comments.

2 Comments

It is not an ArgumentOutOfRangeException error, all my strings are fine. The problem occurs often in razor. When razor has a problem while rendering it just doesnt return any header, not even the asp.net error page, so the page seems to keep rendering. The string is html.
I believe it has something to do with the fact that the properties in the model are linked to a datacontext and handled like dynamics. When razor executes its viewrendering, it cannot pass the dynamic properties to the function, and it crashed. But I am not sure
4

Why not simply substring your article.Intro directly?

@MvcHtmlString.Create(article.Intro.Substring(0, 50))

11 Comments

This can cause incorrect behavior. What if substring at position 49 starts a new table tag, then the output will be "...blablabla <tabl" since MvcHtmlString doesnt notice the whole tag
That problem exists either way; MvcHtmlString simply tells the view engine not to HtmlEncode the string value - aka "<span>" doesnt become "&lt;span&gt;".
This doesn't work either. I tried this first. Even when all my strings are greater then 20 en I do article.Intro.Substring(0, 2) The page gets in endless loop. I think it has more to do with th way razor binds the data. I already solved it another way but I want to know why this happens (It hppens often)
I think something else is going on here if a simple substring call is causing an endless loop. If you remove the substring, it renders fine?
Yes, then it works fine, when I do substring(0) also fine substring(more then 0,int) ... not fine
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.