2012-08-24 57 views
-1

我有格式如ASP MVC3如何添加系統當前日期?

@using (Html.BeginForm()) { 

    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Course</legend> 

     @Html.HiddenFor(model => model.CId) 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.CName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.CName) 
      @Html.ValidationMessageFor(model => model.CName) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Cteator) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Cteator) 
      @Html.ValidationMessageFor(model => model.Cteator) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.date) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.date) 
      @Html.ValidationMessageFor(model => model.date) 
     </div> 

     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 
} 

我想插入系統的當前日期和時間日期字段的字段。我怎樣才能做到這一點。模型和cotroller已經由腳手架創建。

回答

2

插入此調用編輯器(假定你的財產是一個DateTime

@{ Model.date = DateTime.Now; } 

如果你的財產是一個字符串之前,打電話DateTime.Now.ToString()

1

你可以在相應的控制器動作設定的默認值:

public ActionResult Index() { 
    return View(new SomeModel { date = DateTime.Now }); 
} 
1

更改您的代碼,提到以下

@{ Model.date = DateTime.Now.ToString(); } 
@Html.ValidationSummary(true) 
<fieldset> 
    <legend>Course</legend> 

    @Html.HiddenFor(model => model.CId) 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.CName) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.CName) 
     @Html.ValidationMessageFor(model => model.CName) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Cteator) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Cteator) 
     @Html.ValidationMessageFor(model => model.Cteator) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.date) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.date) 
     @Html.ValidationMessageFor(model => model.date) 
    </div> 

    <p> 
     <input type="submit" value="Save" /> 
    </p> 
</fieldset> 
+0

編譯錯誤 說明:資源所需的編譯過程中出現錯誤爲此請求提供服務。請查看以下具體的錯誤細節並適當修改您的源代碼。 編譯器錯誤消息:CS1501:沒有過載的方法爲 '寫入' 取0參數 源錯誤: .................... 第11行: 線12:@using(Html.BeginForm())第13行:@ {Model.date = DateTime.Now; } 第14行:@ Html.ValidationSummary(true) 第15行:

相關問題