0

The built-in EditorFor does only work with member access expressions.

But in one scenario, I need to multiply the value with 100 and have that inside a text box. That is because the value represents a percentage and the user should be able to input 27 which is internally stored as 0.27. For this it would be nice to have something like EditorFor(m => m.MyValue * 100).

Is there any equivalent for that?

1
  • IMHO that would not only be very hard to implement, it would also be very wrong. Conversion of input values is not a responsibility of the View layer. Also, maintaining view code on ASP.NET MVC is already hard enough, please avoid placing C# code in there as much as you can. Commented Jan 2, 2014 at 12:42

2 Answers 2

1

Just add something like MyValueAsPercentage to your view model:

public class MyDataModel
{
    public double MyValue { get; set; }
}

// ...

public class MyViewModel
{
    public double MyValueAsPercentage { get; set; }

    public MyViewModel(MyDataModel dataModel)
    {
        this.MyValueAsPercentage = dataModel.MyValue * 100;
    }

    public MyDataModel ToDataModel()
    {
        return new MyDataModel() { MyValue = MyValueAsPercentage / 100 };
    }
}

Now you can use the same member access expression over your view model:

Html.EditorFor(m => m.MyValueAsPercentage)

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

This is my current approach, but i would like to know if it is possible without a dummy property.
@wertzui Well, the ModelMetadata.FromLambdaExpression which is used internally, only understands expressions of type "member access", "array index" and "indexer".
I thought that maybe someone came up with something like EditorForCustomExpression(), like someone already came up with EditorForMany() to implement custom templates with Lists.
@wertzui Sure, you can make a custom version, but in this case you'd at least need to initialize model metadata "your own way". It seems to me like a little bit of an overhead considering what you're trying to achieve. Personally, I don't see why having a special view model property is a bad approach since the view model is basically a version of your data entry "tuned" for viewing/editing.
1

You can try adding an attribute to your property

[DisplayFormat(DataFormatString = "{0:P2}", ApplyFormatInEditMode = true)]
    public decimal MyValueASPercentage { get; set; }

Now a value of 0.27 will appear as 27 % on your page. Hope that helps

2 Comments

Wow, that looks neat but will MyValueAsPercentage have 0.27 when I type 27% or will it be 27?
I need it without the '%'-sign. Additionally I'm looking for a solution without having to modify the model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.