ASP MVCビューのフィールドに現在の日付のデフォルト値を指定したいのですが、ビューコードでこれを行う方法がわかりません。ただし、これが常に最新の日付であるとは限らないため、これをフィールドで更新できるようにする必要があります。助言がありますか?
<div class="M-editor-label">
Effective Date
</div>
<div class="M-editor-field">
@Html.EditorFor(model => model.EffectiveDate)
@Html.ValidationMessageFor(model => model.EffectiveDate)
</div>
編集
私はこのフィールドをモデルのデフォルト値にしようとしました
private DateTime? effectiveDate = DateTime.Now;
public Nullable<System.DateTime> EffectiveDate
{
get { return DateTime.Now; }
set { effectiveDate = value; }
}
ただし、get
プロパティは次のエラーメッセージを表示します。
Monet.Models.AgentTransmission.EffectiveDate.get must declare a body because it is not marked abstract extern or partial
^(Monetはプロジェクトの名前であり、AgentTransmissionは私が作業している現在のモデルの名前であり、EffectiveDateはプロパティです。)
2番目の編集
以下の回答の1つにある提案に従って、コンストラクターをそのように設定しましたが、ビューがレンダリングされたときに、フィールドに空白の値が入力されました。
public AgentTransmission()
{
EffectiveDate = DateTime.Now;
}
3番目の編集
上記の問題を修正し、get
これまでにコントローラーにあるもの全体を投稿しました。
public AgentTransmission()
{
EffectiveDate = DateTime.Today;
this.AgencyStat1 = new HashSet<AgencyStat>();
}
//Have tried with an without this and got the same results
private DateTime? effectiveDate = DateTime.Today;
public Nullable<System.DateTime> EffectiveDate
{
get { return effectiveDate; }
set { effectiveDate = value; }
}