我想在我的ASP MVC視圖中給出一個字段當前日期的默認值,但我無法弄清楚如何在View代碼中執行此操作。我需要允許這個字段是可更新的,但是,因爲它並不總是最新的日期。有什麼建議麼?在ASP MVC3中設置'editor-field'值查看
<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 is th項目電子名稱,AgentTransmission是當前的模型,我在工作,其中EFFECTIVEDATE是屬性的名稱。)
第二個編輯
每建議在其中一個答案下面我設置構造函數也是如此,但是當渲染視圖時,這仍然會在該字段中留下一個空白值。
public AgentTransmission()
{
EffectiveDate = DateTime.Now;
}
第三編輯
修正上述問題與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; }
}
試圖理解......你是說你想默認或重寫model.EffectiveDate的值爲DateTime.Today? – Peter
我想默認它到今天。 – NealR
(編輯該問題) – NealR