5

I'm working on an mvc .net web application and I'm using Entity Framework for generating Model. I have classes that contain attributes that are doubles. My problem is that when I use @HTML.EditorFor(model => model.Double_attribute) and test my application I can't type a double in that editor, I only can type integers. (I'm using Razor engine for views)How to solve this? Thanks.

Update : I discovered that I can type a double having this format #,### (3 numbers after the comma but I do not want to make user type a specific format, I want to accept all formats (1 or more numbers after the comma) Does anyone have an idea how to solve this? Regards

  • 1
    Is client validation enabled? Did you try to use different decimal separators: . and , (point and comma)? – STO Sep 03 '12 at 17:35
  • This is abnormal it works, you probably have some JS scripts which create an issue. Remove js reference and try to see. – Hassan Sep 03 '12 at 20:25
  • yes it is activated. I tryed with both of them but it didn't work –  Sep 03 '12 at 20:25
  • 1
    @HediNaily, Can you also post your model definition? – Bishnu Paudel Sep 04 '12 at 01:19
  • It shouldn't make a difference but have you tried, `@HTML.EditorFor(model => model.Double_attribute)`? Also, post your model code, do you have any attributes on `Model.Double_attribute`? – Garrett Fogerlie Sep 04 '12 at 02:06

2 Answers2

2

You could use add notations :

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

And now... voila : you can use the double in your view :

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

For other formats you could check this or just google "DataFormatString double" your desired option for this field.

Community
  • 1
  • 1
Mihai Labo
  • 1,082
  • 2
  • 16
  • 40
  • I can only type a double having this format #,### (3 numbers after the comma but I do not want to make user type a specific format, I want to accept all formats (1 or more numbers after the comma) –  Sep 07 '12 at 11:21
0

try to use custom databinder:

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

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

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

and register binder in global.asax:

protected void Application_Start ()
{
    ...
    ModelBinders.Binders.Add( typeof( double ), new DoubleModelBinder() );
}
Xordal
  • 1,369
  • 13
  • 29