我有一個頁面在asp.net MVC獲取波斯的日期時間,並將其存儲在數據庫中的聖誕節datetime.I改變我的文化波斯爲了顯示波斯語言的日期和時間,一切工作正常,我可以存儲日期時間DateTime.Now在數據庫中,但是當我想從用戶在文本框中獲取日期時間(用戶輸入帶有波斯日期時間)並存儲它時,我得到一個超出範圍錯誤說存儲日期時間與自定義文化
一個DATETIME2數據類型爲datetime數據類型的轉換導致外的範圍內的值
我試圖噸Ø從的FormCollection對象獲取時間值,並將其轉換爲datetime,但我沒有運氣,
這是我的看法:
<form action="/Accounting/TempDocument/Insert" method="post" id="insert-form">
<div class="modal-body">
<div class="row">
<div class="form-body">
@Html.AntiForgeryToken()
<div class="form-group form-md-line-input">
@Html.LabelFor(m => m.Description, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextAreaFor(m => m.Description, new { @class = "form-control", @placeholder = "Description" })
<div class="form-control-focus"> </div>
@Html.ValidationMessageFor(m => m.Description, "", new { @class = "help-block" })
</div>
</div>
<div class="form-group form-md-line-input">
@Html.LabelFor(m => m.DocumentDate, new { @class = "col-md-2 control-label" })
<!-- this block is for document date-->
<div class="col-md-10">
@Html.TextBoxFor(m => m.DocumentDate, new { @class = "form-control date-mask", @placeholder = "Document Date" })
<div class="form-control-focus"> </div>
@Html.ValidationMessageFor(m => m.DocumentDate, "", new { @class = "help-block" })
</div>
</div>
</div>
<!-- my form continues -->
這是我的行動:
[Route("Insert")]
[HttpPost]
public async Task<ActionResult> InsertAsync(AccountingDocument model, FormCollection collection)
{
try
{
// much code here
await _service.EntInsertAsync(model);
if (await _service.CommitAsync())
await SetResultMessageAsync("");
}
catch
{
}
return RedirectToAction("RenderTop");
}
我的Global.asax類:
protected void Application_BeginRequest()
{
var persianCulture = new PersianCulture();
Thread.CurrentThread.CurrentCulture = persianCulture;
Thread.CurrentThread.CurrentUICulture = persianCulture;
}
,最後我persianCulture類:
public class PersianCulture : CultureInfo
{
private readonly Calendar cal;
private readonly Calendar[] optionals;
public PersianCulture()
: this("fa-IR", true)
{
}
public PersianCulture(string cultureName, bool useUserOverride)
: base(cultureName, useUserOverride)
{
//Temporary Value for cal.
cal = base.OptionalCalendars[0];
//populating new list of optional calendars.
var optionalCalendars = new List<Calendar>();
optionalCalendars.AddRange(base.OptionalCalendars);
optionalCalendars.Insert(0, new PersianCalendar());
Type formatType = typeof(DateTimeFormatInfo);
Type calendarType = typeof(Calendar);
PropertyInfo idProperty = calendarType.GetProperty("ID", BindingFlags.Instance | BindingFlags.NonPublic);
FieldInfo optionalCalendarfield = formatType.GetField("optionalCalendars",
BindingFlags.Instance | BindingFlags.NonPublic);
//populating new list of optional calendar ids
var newOptionalCalendarIDs = new Int32[optionalCalendars.Count];
for (int i = 0; i < newOptionalCalendarIDs.Length; i++)
newOptionalCalendarIDs[i] = (Int32)idProperty.GetValue(optionalCalendars[i], null);
optionalCalendarfield.SetValue(DateTimeFormat, newOptionalCalendarIDs);
optionals = optionalCalendars.ToArray();
cal = optionals[0];
DateTimeFormat.Calendar = optionals[0];
DateTimeFormat.MonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
DateTimeFormat.MonthGenitiveNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
DateTimeFormat.AbbreviatedMonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
DateTimeFormat.AbbreviatedMonthGenitiveNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
DateTimeFormat.AbbreviatedDayNames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };
DateTimeFormat.ShortestDayNames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };
DateTimeFormat.DayNames = new string[] { "یکشنبه", "دوشنبه", "ﺳﻪشنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه" };
DateTimeFormat.AMDesignator = "ق.ظ";
DateTimeFormat.PMDesignator = "ب.ظ";
}
public override Calendar Calendar
{
get { return cal; }
}
public override Calendar[] OptionalCalendars
{
get { return optionals; }
}
}
我想「正確的」方法是將你的波斯日期轉換爲格利高利(也許使用NodaTime)並存儲它。顯示之前,請翻回來。 –
我正在這樣做,就像翻譯日期時間,當我想向用戶展示時,但是當我開始使用Devexpress組件時,爲了更改devexpress語言,我必須更改文化。 –