2017-08-10 7 views
1

属性を作成することができます。 [AttachDatePicker]:ViewModelの日時プロパティをデコレートします。 datepickerを添付したいhtml要素のhtml属性としてdata-datepicker。htmlデータ属性を出力するためのカスタムモデル属性

私はそれをマイクロソフトの例hereに基づいて行うことができましたが、必要な検証に関連していないので、まったく気にしません。

理想的には、カスタムテンプレートの作成を避けたいと考えています。何か案は?

+0

私はあなたがemplating:https://stackoverflow.com/questions/37674824/templating-in-asp-net-core –

答えて

0

Editor Templateを定義し、DateTimeプロパティの属性を[UIHint]と設定するのはなぜですか?

例えば、[UIHint("{name of editor template}")]

[Required] // example of an annotation you might want to handle 
[UIHint("Datepicker")] 
public DateTime SelectedDate { get; set; } 

EditorForを持つプロパティをレンダリング使用してビュー/共有/ EditorTemplates/Datepicker.cshtml

@model DateTime 

@{ 
    IDictionary<string, object> htmlAttributes = new Dictionary<string, object>(); 
    // TODO: retrieve annotations from Model Metadata and add html attributes accordingly, e.g. "required" 
    htmlAttributes.Add("data-datepicker", ""); 
} 

<div class="@* TODO: set classes depending on your CSS framework *@"> 
    @Html.TextBox("", Model, htmlAttributes) 
</div> 

属性あなたのviewmodelsを作成し、これは正しいテンプレート

に派遣します
@Html.EditorFor(m => m.SelectedDate)