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
This is the model I have:
[Display(Name = "Hardware purchasing")]
[DataType(DataType.Currency)]
public decimal Hardware { get; set; }
And this the view
<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>
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.
If you are facing like this in textbox from the database
Then we need to create model for custom model binders
Model
After that you have to register it with globle.asax file on application start.
In this way you will get the Hardware field will now accept both 2200800.50 and 2,200,800.50.
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:
create a custom model binder as below :
register it to Application_Start() in Global.asax
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
This is not an answer. I want to complete the information... First of all, thank your for your answers. My controller's name is ConsultasController and it is made from the default .net mvc code after create it with view and Entity Framework option. This the code I want to use with edit function:
I have not modify anything important on it. The result at the view should appear as 123,23€, with commas separating decimals and I need that it will be translated on 133.23€ with point that's sql BD accepts. I have observe your answer, both, but I can't understand where I have to put theses each codes. Have I to made a new model o insert them into de the controller? Thank you!