1

DateTimeのエディターテンプレートを作成したいですか?日、月、年の3つのテキストボックスがあるフィールド。現在、私のEditorTemplates/Date.cshtmlは次のようになっています。

@Html.TextBox(string.Empty, Model.HasValue ? Model.Value.Day.ToString() : "",
                            new { @class = "day-box", title = "day", min = "1", max = "31", type = "number" })
@Html.TextBox(string.Empty, Model.HasValue ? Model.Value.Month.ToString() : "",
                            new { @class = "month-box", title = "month", min = "1", max = "12", type = "number" })
@Html.TextBox(string.Empty, Model.HasValue ? Model.Value.Year.ToString() : "",
                    new { @class = "year-box", title = "year", min = "1900", max = "2020", type = "number" })

明らかな問題は、(そのままで)id属性とname属性がすべて同じ値に設定されることです。これらの値に何かを追加する必要があり、モデルに戻さないでください。

idに_Dayを追加し、Day入力の名前に.Dayを追加すると、同様に月と年で問題が解決することを期待していました。しかし、私はこれを行う簡単な方法を見ることができません。

実際の値に非表示の入力を使用し、次にjavascriptを使用して、3つの(d / m / y)テキストボックスのいずれかで値が変更されるたびに非表示フィールドの値を更新しました。

それで、string.Emptyを渡すときに、MVCが使用する名前とIDを取得する方法はありますか?ViewData.TemplateInfo.HtmlPrefixを使用しても、完全な値が得られないようです。

私がやりたいことをするためのより良い方法はありますか?私がこれに最初に取り組んだとは想像できません。

私はそこにサードパーティの日付ピッカーがあることを知っています。この問題のために、私はそれらに興味がありません。

ありがとう、〜S

4

2 に答える 2

1

DateTime の Year Month Day が設定可能であれば、DateTime.cshtml EditorTemplates でこれを行うことができます

@model System.DateTime
@Html.TextBoxFor(model => model.Year) / @Html.TextBoxFor(model => model.Month) / @Html.TextBoxFor(model => model.Day)

上はありえない。DateTime の Year、Month、および Day プロパティは読み取り専用です。

あなたができることは、別のデータ型を作成することです:

public struct DatePart
{
    public int Year { get; set; }
    public int Month { get; set; }
    public int Day { get; set; }

    public DateTime Date { get { return new DateTime(Year, Month, Day); } }
}

次に、あなたの日付でこれを行います

@model TestV.Models.DatePart           
@Html.TextBoxFor(model => model.Year) / @Html.TextBoxFor(model => model.Month) / @Html.TextBoxFor(model => model.Day)

次に、DatePart の Date(DateTime type) プロパティにアクセスします。

于 2012-08-04T04:59:34.320 に答える
0

JavaScript を使用してカスタム テンプレートの 3 つのフィールドから日付を形成し、それを Model.Date にバインドする隠しフィールドに書き込むことができます。

于 2012-08-04T04:22:24.550 に答える