在C Sharp MVC上使用逗号发送十进制数 [英] Send decimal number with commas working on c sharp mvc

查看:56
本文介绍了在C Sharp MVC上使用逗号发送十进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在这个非常有趣的论坛中。
我正在做一个mvc项目。我有我的模型,并且在其中有一个用于货币的十进制属性。我也有使用Html Helper TextEditorFor的编辑视图。该HTML帮助器在网页上显示了de Database中的一个值,但将其显示为带逗号的十进制。然后,当我要再次发送此编辑但不进行任何编辑时,它不会将任何内容发送到数据库,并显示错误的TextEditBox,在其中我需要更改逗号才能将数据发送到数据库。我希望我能正确解释它:P

This is my first time in this very interesting forum. I'm working on a mvc project. I have my model and inside it one decimal property for currency. Also I have its edit view in which I use Html Helper TextEditorFor. This Html Helper shows at the webpage one value from de Database but it shows it as a decimal with comma. Then, When I want to send again this edit without edditing nothing, it doesn't send anything to the Database and shows the wrong TextEditBox in which I need to change the commas for a point to be able to send this data to the Database. I hope I'd have explained it correctly :P

这是我所拥有的模型:

[Display(Name = "Hardware purchasing")]
[DataType(DataType.Currency)]
public decimal Hardware { get; set; }

这是视图

<div class="form-group ">
@Html.LabelFor(model => model.Hardware,  htmlAttributes: new { @class = "control-label col-md-2 col-xs-6" })
<div class="col-md-1 col-xs-3 ">
    @Html.EditorFor(model => model.Hardware, new { htmlAttributes = new { @class = "form-control text-center" } })
    @*@Html.ValidationMessageFor(model => model.Hardware, "", new { @class = "text-danger" })*@
</div>

我正在寻找一种方法,该方法可让我将此数字(带逗号)转换为十进制,以便发送至数据库。也许在控制器中获取html值并将其转换,但是我不知道这是否是正确的方法。
我希望你能帮助我。非常感谢。

I was looking for some method that let me convert this number with commas to decimal in order to sent to the database. Perhaps getting the html value in the controller and converting it, but I don't know how neither if this is the correct way. I hope you could help me. Thank you very much.

推荐答案

默认的MVC模型绑定程序无法解析显示格式的值。因此,可能您最终将为Model类编写自己的Binder并在Application_Start中注册:

Default MVC model binder cannot parse value formatted for display. So,Probably you will end up writing your own Binder for your Model class and register in Application_Start:

public class Test
    {
        [Display(Name = "Hardware purchasing")]
        [DataType(DataType.Currency)]
        public decimal Hardware { get; set; }
    }

创建如下所示的自定义模型联编程序:

create a custom model binder as below :

    public class TestModelBinder : DefaultModelBinder
     {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                var result = bindingContext.ValueProvider.GetValue("Hardware");

                if (result != null)
                {
                    decimal hardware;
                    if (Decimal.TryParse(result.AttemptedValue, NumberStyles.Currency, null, out hardware))
                    return new Test { Hardware = hardware };

                bindingContext.ModelState.AddModelError("Hardware", "Wrong amount format");
            }

            return base.BindModel(controllerContext, bindingContext);
        }
    }

将其注册到Global.asax中的Application_Start()

register it to Application_Start() in Global.asax

ModelBinders.Binders.Add(typeof(Test), new TestModelBinder());

只需如上所述编写ModelBinder,然后在您的控制器操作方法中,让框架知道什么模型要使用的联编程序,即不是默认设置,而是自定义的

Just Write the ModelBinder as mentioned above and then in your controller action method.Let the framework know what model binder to use i.e. Not the default but your custom one

public ActionResult Edit([ModelBinder(typeof(TestModelBinder ))] TestModelBinder test)

这篇关于在C Sharp MVC上使用逗号发送十进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆