简体   繁体   English

如何使用razor DisplayFor格式化mvc页面中的显示文本?

[英]How to format display text in mvc page using razor DisplayFor?

I am working on asp.net mvc3 application. 我正在asp.net mvc3应用程序上工作。

In this user can post data, i am using textarea for input. 在该用户可以发布数据的情况下,我正在使用textarea进行输入。 This data is stored into database and i am retrieving it using razor @Html.DisplayFor(). 此数据存储到数据库中,我正在使用剃刀@ Html.DisplayFor()进行检索。

My Data for textarea is like this (inputed by user) 我的文本区域数据是这样的(由用户输入)

Oh woooah, oh woooooah, oh wooooah, oh.
You know you love me, I know you care,
you shout whenever and I’ll be there.
You are my love, you are my heart
and we will never ever ever be apart.
Are we an item? girl quit playing,
we’re just friends, what are you saying.
Said there’s another, look right in my eyes,
my first love broke my heart for the first time.
And I was like…


Baby, baby, baby oooooh,
like baby, baby, baby noooooooo,
like baby, baby, baby, ooooh.
Thought you’d always be mine, mine (repeat)

and when i retrieve it using this, 当我使用它检索它时

<em>@Html.DisplayFor(model => item.inputvalue)</em>

but it is displayed as unformated text like below, 但显示为未格式化的文本,如下所示,

Oh woooah, oh woooooah, oh wooooah, oh. 哦,哦,哦,哦,哦,哦。 You know you love me, I know you care, you shout whenever and I'll be there. 你知道你爱我,我知道你在乎,你大喊大叫,我会在那里。 You are my love, you are my heart and we will never ever ever be apart. 你是我的爱,你是我的心,我们将永远不会分开。 Are we an item? 我们是物品吗? girl quit playing, we're just friends, what are you saying. 女孩退出游戏,我们只是朋友,你在说什么。 Said there's another, look right in my eyes, my first love broke my heart for the first time. 说还有另外一个,在我的眼中,我的初恋第一次让我心碎。 And I was like… Baby, baby, baby oooooh, like baby, baby, baby noooooooo, like baby, baby, baby, ooooh. 我就像……婴儿,婴儿,婴儿oooooh,像婴儿,婴儿,婴儿noooooooo,像婴儿,婴儿,婴儿,ooooh。 Thought you'd always be mine, mine (repeat) 以为你永远是我的,我的(重复)

How can i format display text? 如何格式化显示文字? Is there any property to display this as formated text? 是否有任何属性可将其显示为格式化文本?

You should replace new lines with <br/> . 您应该用<br/>替换新行。 You could write a custom helper to do this: 您可以编写一个自定义帮助程序来执行此操作:

public static class HtmlExtensions
{
    public static IHtmlString DisplayFormattedData(this HtmlHelper htmlHelper, string data)
    {
        if (string.IsNullOrEmpty(data))
        {
            return MvcHtmlString.Empty;
        }

        var result = string.Join(
            "<br/>", 
            data
                .Split(new[] { Environment.NewLine }, StringSplitOptions.None)
                .Select(htmlHelper.Encode)
        );
        return new HtmlString(result);
    }
}

and then: 接着:

@Html.DisplayFormattedData(item.inputvalue)

I made few changes in Darin Dimitrov's answer. 我对达林·迪米特洛夫(Darin Dimitrov)的回答几乎没有改变。 Its now working perfect.. 现在可以正常工作了。

Thanks Darin Dimitrov for your help. 感谢Darin Dimitrov的帮助。

public static class HtmlExtensions

    {
        public static IHtmlString DisplayFormattedData(this HtmlHelper htmlHelper, string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                return MvcHtmlString.Empty;
            }

            string myString=data;
                myString = myString.Replace("\n", "<br>");

                return new HtmlString(myString);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM