&を使用して、オブジェクトからの形式でMVC 4
時間のみを表示するエディター テンプレートを作成しましたHH:mm
DateTime
エディター テンプレート コード (TimePicker.cshtml)
@model DateTime?
@{
String modelValue = "";
var dateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;
var id = "";
var cls = "";
var style = "";
var placeholder = "";
if (ViewData["_id"] != null)
{
id = (string)ViewData["_id"];
}
if (ViewData["_class"] != null)
{
cls = (string)ViewData["_class"];
}
if (ViewData["_style"] != null)
{
style = (string)ViewData["_style"];
}
if (ViewData["_placeholder"] != null)
{
placeholder = (string)ViewData["_placeholder"];
}
if (Model.HasValue)
{
if (Model.Value != DateTime.MinValue)
{
modelValue = Model.Value.ToString("HH:mm", dateFormat);
}
}
}
@Html.TextBox("", modelValue,
new { @class = cls, @id = id, @style = style, @placeholder = placeholder })
ビューで
@Html.EditorFor(m => m.StartTime, "TimePicker", new { _id = "StartTime"})
しかし、レンダリングされた最終的な HTML は
<input id="StartTime" name="StartTime" type="text" value="2013-08-05 0:00:00" />
以下のようにデバッグ用の値をハードコーディングしてみました
@Html.TextBox("", "00:00",
new { @class = cls, @id = id, @style = style, @placeholder = placeholder })
それでも私は2013-08-05 0:00:00
代わりに同じことを得る0:00
コードの問題は何ですか?
EDITadding name parameter
: TextBoxへのクイックウォッチで結果が得られます。しかし、名前属性を追加しますが、これは望ましくありません。このスクリーンショットを見つける