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.NETMVCに見られるように-強く型付けされたTextAreaにデフォルト値を設定するにはどうすればよいですか?、モデル変数がコントローラーに返送されるかどうかわからないため、このメソッドを使用できるかどうかはわかりません。

また、もう1つの簡単な質問(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
    ...
}

もう 1 つの簡単な質問 (QuestionCeption) これらの textAreas からデータベースにデータを保存すると (SQL Server Express 2008 を使用)、改行文字が保存されますか?

はい、変換を行わずにテキストをそのままデータベースに保存するだけです。これは、後でテキストエリアにテキストを表示するときに機能しますが、たとえば に表示したい場合は<div>、HTML エンコードして新しい行を<br/>. たとえば、このジョブのカスタム HTML ヘルパーを作成できます。

于 2012-05-07T10:06:24.563 に答える