Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor CSHTML IF statement

Tags:

razor

Anyone here that can help me? I have the following code:

@inherits umbraco.MacroEngines.DynamicNodeContext
@{ 
    var node = @Model.NodeById(1257);
}
    <div class="Top10"> 
    <h1>Newest</h1>

@foreach (var article in node.Descendants().Where("Visible && (NodeTypeAlias = \"Article\" || NodeTypeAlias = \"sergrein\" || NodeTypeAlias = \"solomyndagrein\")").OrderBy("createDate desc").Take(10))
    {                   
         <a href="@article.Url"><h2>@article.createDate.ToString("dd/MM") | @article.title</h2></a>    
    }

    </div>

What I want is: if @article.title is longer than e.g. 10 characters, it needs to return the 10 characters followed by ... (for example: "this_is_a_..."). If the @article.title is shorter than 10 characters, it can just show the full title length. How can this truncating be done?

like image 328
user3042087 Avatar asked Nov 27 '13 14:11

user3042087


1 Answers

Try this

@(article.title.Length > 10 ? (article.title.Substring(0,10) + " ...") : article.title)
like image 73
Yevgeniy.Chernobrivets Avatar answered Oct 12 '22 09:10

Yevgeniy.Chernobrivets