0

ASP.NET MVC4 を使用して、ユーザーが Author、Text、および CreateTime を指定する CreateView を作成しました。ユーザーに CreateTime を表示したり入力したりしたくありません。自分のコードでDateTime.Now.ToShortDateTime().

それは私のCreateViewのこの部分です。

@Html.EditorFor(model => model.CreateTime)

編集: ここに私の CreateView があります:

@model Models.Topic


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Topic</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Author)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Author)
            @Html.ValidationMessageFor(model => model.Author)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CreateTime)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CreateTime)
            @Html.ValidationMessageFor(model => model.CreateTime)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
4

4 に答える 4

2

エディターのデフォルト値を設定できます

@Html.EditorFor( model => model.CreateTime, 
                                        new { @Value = DateTime.Now.ToShortDateString() })
于 2013-09-21T01:10:41.227 に答える
2

html ヘルパーを介して非表示の入力を作成します。

@Html.HiddenFor(model => model.CreateTime)
于 2013-09-21T01:07:34.493 に答える