質問

I have created a custom Display For template that will be used mainly in my index file so that when the records are shown in the lists, they are not turned into ugly looking creature if some records are way too lengthy. I have tried following:

@model string

@{
    string text = Html.Encode(Model??"");
    if (text.Length >= 35)
    {
        text = text.Substring(0, 35)+"...";

    }    
    @Html.DisplayFor(model=>text) 
 }

Though it works fine for the strings having length more than 35 or equal to it, but it doesn't work if the string is lesser than that. I have tried the else statement, but it doesn't work either. What is the correct way to do this?

Edit: Null string. In the source page file, between the two there is nothing.

役に立ちましたか?

解決

try this for the template

@model string
@{
    string text = Html.Encode(Model ?? "");
    if (text.Length >= 35)
    {
        text = text.Substring(0, 35) + "...";
    }    
}
@text
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top