15

I have a field for weight in Kgs (type double or use something else??). In edit view I would like the user to enter numbers to the thousandth place. In display view I would like the Kgs to appear like 560.250

Trying to learn MVC3 + Razor. Willing to explore JQuery, use of regular expressions, validators, view templates, view models...

The "magic" of MVC based on conventions takes getting used to. Confused as to which approach to use.

Thank you in advance for your help.

Par6
  • 389
  • 1
  • 10
  • 20
  • 2
    For people who are looking as long for the resource with all available string formats as I was, here is the MSDN article: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx – Daniël Tulp Oct 22 '13 at 14:18

3 Answers3

34

You could use data annotations on your view model:

[DisplayFormat(DataFormatString = "{0:#,##0.000#}", ApplyFormatInEditMode = true)]
public double? Weight { get; set; }

and in your view

@Html.EditorFor(x => x.Weight)

will properly format the value in the input field.

Another possibility is to write a custom editor template for the double type (~/Views/Shared/EditorTemplates/double.cshtml):

@model double?
@Html.TextBox("", Model.HasValue ? Model.Value.ToString("#,##0.000#") : "")

and then in your view:

@Html.EditorFor(x => x.Weight)

or if you don't want to override all templates for all double types in your application you could put this into some custom template location like ~/Views/Shared/EditorTemplates/MyFormattedDouble.cshtml and then in your view:

@Html.EditorFor(x => x.Weight, "MyFormattedDouble")

Personally I prefer the first approach which uses data annotations to control the format of the double values.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thank you. that pretty much sums up the options. The customer editor template approach is intriguing but I agree that the data annotations make is so simple. – Par6 Sep 11 '11 at 22:28
  • Hi, Thanks, it works just at loading data, by posting back the same value with thousands separator it raises error that the value must be a number. how to overcome this issue? – mahdi gh Mar 10 '14 at 06:21
  • @mahdigh, you could write a custom model binder. – Darin Dimitrov Mar 10 '14 at 15:10
4

To format the number just use

 @string.Format("{0:0.00}", Model.Weight);

or

 @Html.DisplayFor(x => string.Format("{0:0.00}", x.Weight));
 @Html.EditorFor(x => string.Format("{0:0.00}", x.Weight));

to Validate

public class Model
{
    [Required]
    public double Weight{ get; set; }
}

I wouldn't constrain the precision they put in, just make sure that it is a valid number using javascript. You might also constrain input to only include numbers and a period.

If the user puts in something wrong (i.e. not compatible with a double type), MVC will complain when it tries to bind to the model.

Manual5355
  • 981
  • 10
  • 27
  • Thank you for the response. I'm getting one of these error msg: _Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions_ Original Code: `@Html.DisplayFor(modelItem => item.WeightKgs)` – Par6 Sep 11 '11 at 02:02
  • @Par6 change it to @Html.DisplayFor(modelItem => modelItem.WeightKgs) – mnsr Sep 11 '11 at 02:16
  • @rudeovski it is actually a part of a loop. `@foreach (var item in Model) {...}` – Par6 Sep 11 '11 at 03:02
  • I just tried this (using DataAnnotations on the model) and it worked... but again, I don't know if it's the right way of doing things. `[DisplayFormat(DataFormatString= "{0:#,##0.000#}")] public Nullable WeightKgs { get; set; }` – Par6 Sep 11 '11 at 03:19
  • What is your Model? If it's not a Collection (i.e. list, array, etc) you shouldn't be using @foreach(var item in Model). Otherwise that for loop doesn't have anything to iterate over. – Manual5355 Sep 11 '11 at 13:57
0

its very simple follow this method

so you have to insert DataFormatString="{0:#,##0.000#Kg}" only on gridview

Sakthi Karthik
  • 3,105
  • 4
  • 25
  • 29