我需要在
中使用strvalue而不是value var date = (DateTime)value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture);
在以下代码中。实际上我需要使用波斯日期时间选择器,我需要将我的数字转换为英语,所以我强制将值转换为字符串来替换字符。
public class DateTimeBinder : IModelBinder
{
private static readonly CultureInfo arabic = new CultureInfo("fa-IR");
private static readonly CultureInfo latin = new CultureInfo("en-US");
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
String strvalue = (String)value.ConvertTo(typeof(String), CultureInfo.CurrentCulture);
var latinDigits = latin.NumberFormat.NativeDigits;
var arabicDigits = arabic.NumberFormat.NativeDigits;
for (int i = 0; i < latinDigits.Length; i++)
{
strvalue = strvalue.Replace(arabicDigits[i], latinDigits[i]);
}
var date = (DateTime)value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture);
date = PersianCulture.PersianToGregorianUS(date);
return date;
}
}
答案 0 :(得分:0)
ValueProviderResult value1 = new ValueProviderResult(strvalue,strvalue, CultureInfo.CurrentCulture);
var date = (DateTime)value1.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture);