2016-05-04 25 views
-1

我相信問題已經被問到,但答案不同於我的問題。如何解決System.FormatException發生在mscorlib.dll中,但沒有在用戶代碼中處理

我有一個網格,我從中選擇用戶。當我點擊相應的ID它適用於一些用戶精細和一些它打破,並顯示以下錯誤,

錯誤:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: String was not recognized as a valid DateTime.

我的編碼

<div class="col-md-10"> 
    <input type="text" value='@DateTime.Parse(Model.IncidentDate.ToString()).ToString("dd/mm/yyyy"))' class="form-control input-xs" disabled /> 
</div> 

型號

public string PNumber { get; set; } 
public DateTime? IncidentDate { get; set; } 
public string CNumber { get; set; } 

先進的感謝..

+0

因爲你轉換'DateTime'爲'字符串「,然後使用格式再次將生成的」字符串「轉換爲」字符串「。 - 它只是'Model.IncidentDate.ToString(「dd/mm/yyyy」)'但你認爲這一點 - 你想達到什麼目的? –

+0

您的IncidentDate屬性可以爲空。當IncidentDate的值爲空時,是否拋出異常? – MarcoLaser

+0

@StephenMuecke我想用這種格式的日期。我只是不知道我可以如何工作。 – Mpho

回答

0

嘗試以下操作:

@Html.TextBoxFor(m => m.IncidentDate, "{0:dd/MM/yyyy}", new { @class = "form-control input-xs", disabled = "disabled" }) 

如果沒有值已設置爲IncidentDate那麼它會顯示一個空文本框。如果值已設置,那麼它會顯示一個文本框,在它的日期,例如:

04/05/2016 

上述數值代表如下:

04 - dd 
05 - MM 
2016 - yyyy 
相關問題