Asp.net mvc 如何显示文本区域';表中的数据

Asp.net mvc 如何显示文本区域';表中的数据,asp.net-mvc,asp.net-mvc-2,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 2,Asp.net Mvc 3,我正在使用ASP.MVC3。我有一个视图,上面有一个文本区域。我捕获其中的数据,当我想要一个新段落时,我会按enter键两次。输入所有数据后,我将文本保存到数据库中 在我的详细信息视图中,我将以类似的方式显示数据: <tr> <td valign="top"><label>Body:</label></td> <td>@Model.Body</td> </tr> 正文: @模特。身体

我正在使用ASP.MVC3。我有一个视图,上面有一个文本区域。我捕获其中的数据,当我想要一个新段落时,我会按enter键两次。输入所有数据后,我将文本保存到数据库中

在我的详细信息视图中,我将以类似的方式显示数据:

<tr>
   <td valign="top"><label>Body:</label></td>
   <td>@Model.Body</td>
</tr>

正文:
@模特。身体
现在文本显示为1个段落,即使在我的文本区域(当我捕获数据时),它看起来像段落

如何让数据在表格中显示为段落,就像我在文本区域中捕获的一样。我假设我必须搜索回车并用break标签替换它们

我假设我必须搜索回车并用break标签替换它们

是的,你的假设是正确的。您可以使用自定义HTML帮助器:

public static IHtmlString FormatBody(this HtmlHelper htmlHelper, string value)
{
    if (string.IsNullOrEmpty(value))
    {
        return MvcHtmlString.Empty;
    }
    var lines = value.Split('\n'); // Might need to adapt
    return htmlHelper.Raw(
        string.Join("<br/>", lines.Select(line => htmlHelper.Encode(line)))
    );
}

更新:

下面是一个如何对该方法进行单元测试的示例:

[TestMethod]
public void FormatBody_should_split_lines_with_br_and_html_encode_them()
{
    // arrange
    var viewContext = new ViewContext();
    var helper = new HtmlHelper(viewContext, MockRepository.GenerateStub<IViewDataContainer>());
    var body = "line1\nline2\nline3<>\nline4";

    // act
    var actual = helper.FormatBody(body);

    // assert
    var expected = "line1<br/>line2<br/>line3&lt;&gt;<br/>line4";
    Assert.AreEqual(expected, actual.ToHtmlString());
}
[TestMethod]
public void FormatBody_应该_分割_行_与_br_和_html_编码_它们()
{
//安排
var viewContext=新的viewContext();
var helper=newhtmlhelper(viewContext,MockRepository.GenerateStub());
var body=“line1\nline2\nline3\nline4”;
//表演
var实际值=helper.FormatBody(body);
//断言
var expected=“line1
line2
line3
line4”; Assert.AreEqual(应为,实际为.ToHtmlString()); }
我假设我必须搜索回车并用break标签替换它们

是的,你的假设是正确的。您可以使用自定义HTML帮助器:

public static IHtmlString FormatBody(this HtmlHelper htmlHelper, string value)
{
    if (string.IsNullOrEmpty(value))
    {
        return MvcHtmlString.Empty;
    }
    var lines = value.Split('\n'); // Might need to adapt
    return htmlHelper.Raw(
        string.Join("<br/>", lines.Select(line => htmlHelper.Encode(line)))
    );
}

更新:

下面是一个如何对该方法进行单元测试的示例:

[TestMethod]
public void FormatBody_should_split_lines_with_br_and_html_encode_them()
{
    // arrange
    var viewContext = new ViewContext();
    var helper = new HtmlHelper(viewContext, MockRepository.GenerateStub<IViewDataContainer>());
    var body = "line1\nline2\nline3<>\nline4";

    // act
    var actual = helper.FormatBody(body);

    // assert
    var expected = "line1<br/>line2<br/>line3&lt;&gt;<br/>line4";
    Assert.AreEqual(expected, actual.ToHtmlString());
}
[TestMethod]
public void FormatBody_应该_分割_行_与_br_和_html_编码_它们()
{
//安排
var viewContext=新的viewContext();
var helper=newhtmlhelper(viewContext,MockRepository.GenerateStub());
var body=“line1\nline2\nline3\nline4”;
//表演
var实际值=helper.FormatBody(body);
//断言
var expected=“line1
line2
line3
line4”; Assert.AreEqual(应为,实际为.ToHtmlString()); }
@Darin:如果您需要提供一个HtmlHelper实例,您将如何为此编写测试?@Darin:谢谢。为什么不需要存根ViewContext?@Brendan Vogt,因为在这个自定义帮助器方法中,我们没有使用任何需要这样做的东西。@Darin:我不明白,你能再详细解释一下吗。您删除了1参数,而不是另一个。在本例中,您没有存根ViewContext和存根IViewDataContainer。考虑到这个测试场景,您何时存根,何时不存根?@Darin:考虑到您需要提供一个HtmlHelper实例,您将如何为此编写测试?@Darin:谢谢。为什么不需要存根ViewContext?@Brendan Vogt,因为在这个自定义帮助器方法中,我们没有使用任何需要这样做的东西。@Darin:我不明白,你能再详细解释一下吗。您删除了1参数,而不是另一个。在本例中,您没有存根ViewContext和存根IViewDataContainer。考虑到这个测试场景,您何时存根,何时不存根?