asp.net - mvc textboxfor using an input mask for decimal -


We are using MVC and I want to see if there is another better way to handle this problem. We currently have a decimal field declared in a model named AmountOfContract:

  [DisplayName ("amount of contract")] [DisplayFormat (ApplyFormatInEditMode = true, DataFormatString = "{ 0: c} ")] get public decimal AmountOfContract { Set; }  

was displayed on a page using:

  @ html.LabelFor (model => model.AmountOfContract, new {@ Class = "control -label col-md-2"}) & lt; Div class = "col-md-4" & gt; & Lt; Div class = "input-group m-b" & gt; & Lt; Span class = "input-group-connector" & gt; $ & Lt; / Span & gt; @ Html.TextBoxFor (model = & gt; model.AmountOfContract, new {@class = "form-control"}) @ html.ValidationMessageFor (model => model.AmountOfContract) & lt; / Div & gt; & Lt; / Div & gt;  

It was working fine. Later, our users requested that they have a textbox show (comma), instead of typing 10000.00 instead, they show that fill in commas so that It can be typed in 10,000.00.

We auto numeric .js That mask was implemented, but if we presented the model with a value of 10,000.00 then the model ISIG Wald will always return false with the model. AmountOfContract is being set to 0. This comma is being compelled to be in the decimal area, it is not valid.

The way we got around it, it was to create another area

  [DisplayName ("Amount of contract")] Public String Stormantoff Contract {get ; Set; }  

This is then used on the form, so the user sends and receives a string, which is then receiving the model. But before saving the DB, we have to assign the Stormantoff Contract to allocate.

Is there a better way to do this? As we have some forms, we have to change in the same way.

Values ​​are not accepted with decimal Comma also you can recive the value in the string and convert it ! You can use regular expressions to verify the value:

  [DisplayName ("amount of contract")] [regular expression ("" (((((\ \ {{1, 3}) (\ d {3}) *) | (\ d +)) (. \ D +)? $ ", Error message =" value format should be 00.00 or 00,00 ")] Public string AmountOfContract { Get; set;}  

see

change your action from string to decimal:

  if (ModelState. IsValid) {decimal value = Convert.ToDecimal (model.Price.Replace (',', toString () '.'.);}  

View


Comments

Popular posts from this blog

c# - SignalR: "Protocol error: Unknown transport." when navigating to hub -

c# - WPF Expander overlay used in ItemsControl -

android - how to get distance of 2 beacons and make a condtion base on that distance -