0

好的,这是我的情况,已经到了这个……戏剧性的停顿……

我的 MVC 视图中有以下 TextAreaFor<> 框:

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Monday) %>
        </div>
        <div class="editor-field">
            <%: Html.TextAreaFor(model => model.Wednesday, 6, 40, new { })%>
            <%: Html.ValidationMessageFor(model => model.Monday) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Wednesday) %>
        </div>
        <div class="editor-field">
            <%: Html.TextAreaFor(model => model.Wednesday, 6, 40, new {})%>
            <%: Html.ValidationMessageFor(model => model.Wednesday) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Friday) %>
        </div>
        <div class="editor-field">
            <%: Html.TextAreaFor(model => model.Friday, 6, 40, new {}) %>
            <%: Html.ValidationMessageFor(model => model.Friday) %>
        </div>

对于那些有很多经验的人,你可能已经注意到我使用强类型视图来减少开发时间,所以这里有很多默认项。

我需要知道,如何设置 TextAreaFor<> 框的默认值?IE,我可能想要其中的文本作为模板。用户添加到这些 textAreas 的任何内容都将存储在数据库中(后端已经完成且功能齐全)。正如文章ASP.NET MVC - How can I set the Default Value in a Strongly Typed TextArea? ,我不确定我是否可以使用这种方法,因为我不确定模型变量是否会被发送回我的控制器。

另一个快速问题(QuestionCeption)如果我将这些 textAreas 中的数据保存到数据库中(使用 SQL Server Express 2008),它会保留换行符吗?

这是我需要的默认值示例。

Traditional:
Healthy Lifestyle:
Chill out:
Vegetarian:
Soup:
Sandwich:
No thanks.
4

1 回答 1

3

您可以在呈现此视图的控制器操作中执行此操作:

public ActionResult Index()
{
    MyViewModel model = ...
    model.Friday = "some default value";
    return View(model);
}

并且您将有一个相应的 POST 操作来检索值:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // model.Friday will contain the text entered by the user in the corresponding textarea
    // here you could store for example the values in the database
    ...
}

另一个快速问题(QuestionCeption)如果我将这些 textAreas 中的数据保存到数据库中(使用 SQL Server Express 2008),它会保留换行符吗?

是的,您只需将文本按原样保存在数据库中,无需进行任何转换。当您稍后在 textarea 中显示文本时,这将起作用,但是如果您想在例如 a 中显示它,<div>则必须对其进行 HTML 编码并将新行替换为<br/>. 例如,您可以为此作业编写自定义 HTML 帮助程序。

于 2012-05-07T10:06:24.563 回答