使用逗号MVC 5格式化十进制值

时间:2017-04-29 22:30:59

标签: c# asp.net asp.net-mvc string-formatting

我有问题! 我正在使用Mvc5,我有这样的属性。

[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public decimal Total { get; set; }

剃刀:

@Html.EditorFor(modelItem => Model.Total, new { htmlAttributes = new { @class = "form-control input-sm" } })

此时没有错误。但如果我像1.300,40一样发送,我总是0。

但如果我像1300,40那样发送,我会得到正确的价值。 我该如何解决?如果我发送1300,50或1.300,40

,我想得到正确的值

2 个答案:

答案 0 :(得分:4)

您必须添加自己的ModelBinder

public class DecimalModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        var modelState = new ModelState { Value = valueResult };
        decimal actualValue = 0;

        try
        {
            actualValue = Convert.ToDecimal(valueResult.AttemptedValue,
                CultureInfo.CurrentCulture);
        }
        catch (FormatException e)
        {
            modelState.Errors.Add(e);
        }

        bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
        return actualValue;
    }
}

并在Application_Start中注册:

ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());

参考:http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx/

答案 1 :(得分:0)

如果要对所有c#值类型使用通用ModelBinder,则

<Button title="exec" onPress={() => {
console.log('going...');
  this.abc = setInterval(() => {
    console.log('going...');
  }, 100);
}} />
<Button title="stop" onPress={() => clearInterval(this.abc)} />

添加上述类之后,在Global.asax.cs文件的“ Application_Start”方法下配置ModelBinder。

      NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("VEDRISE",
          "Vedic Notifications",
          NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("Will display daily Panchang notificatons");
        mNotificationManager.createNotificationChannel(channel);
      }
      Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//      URL url = new //URL("https://66.media.tumblr.com/ec76b7d7e0529af6e3a437edb6c6c255/tumblr_phiur//jqePq1xp0noco1_75sq.png");
 //     Bitmap image = //BitmapFactory.decodeStream(url.openConnection().getInputStream());
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "VEDRISE")
        .setSmallIcon(R.drawable.sunrise) // notification icon
  //      .setLargeIcon(image)
        .setContentTitle(title) // title for notification
        .setContentText(content)// message for notification
        .setSound(alarmSound) // set alarm sound for notification
        .setAutoCancel(true); // clear notification after click
      Intent intent = new Intent(context, this.getClass());
      PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      mBuilder.setContentIntent(pi);
      int id = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
      Log.d("VedicHoroo", String.format("id: %d", id));
      mNotificationManager.notify( id, mBuilder.build());