![](/img/trans.png)
[英]How to fetch value from resource file inside JavaScript in ASP.net MVC view using Razor
[英]How to set value into HTML5 date field from Asp.Net MVC Razor?
我在這里發現了一個類似的問題,但這對我的情況不起作用。 請看下面的屏幕截圖。 當我在“編輯”表單中呈現數據時,除了突出顯示的日期字段外,所有數據都被正確設置為控件。 當我檢查元素時,我發現值存在,但由於某種原因它沒有顯示在字段中。
這是我用來在這個日期字段中設置值的代碼。 我嘗試了“yyyy-MM-dd”和“MM / dd / yyyy”格式但沒有效果。
<td>Joining Date</td>
<td>
@{
if(Model.JoiningDate != null)
{
<input id="txtJoiningDate" type="date" style="width: 100%;" value="@String.Format("{0:MM/dd/yyyy}",Model.JoiningDate.Value)" />
}
else
{
<input id="txtJoiningDate" type="date" style="width: 100%;" />
}
}
</td>
編輯
為什么你只使用html helper如圖所示: -
@Html.TextBox("txtJoiningDate", Model.JoiningDate.Value ,"{0:yyyy-MM-dd}",new{ @style="width:100%", type="date" })
使用Html幫助器
@Html.TextBox("txtJoiningDate", Model.JoiningDate.Value ,"{0:MM/dd/yyyy}")
在模型中
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public Nullable<System.DateTime> JoiningDate{ get; set; }
這對我有用
對於Html5輸入類型=“日期”字段,必須以“yyyy-MM-dd”格式指定值。 以該格式設置后,您將看到屏幕上指定的值。
@Html.EditorFor(x => x.JoiningDate)
在你的模型中:
[DisplayName("Joining date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? JoiningDate { get; set; }
你確定你嘗試過yyyy-MM-dd格式嗎? 看看這個小提琴 ,在Chrome上正常工作
<input id="txtJoiningDate" type="date" style="width: 100%;" value="2010-12-25" /> -> works
<input id="txtJoiningDate" type="date" style="width: 100%;" value="2010/12/25" /> -> will not work
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.