Reputation: 5750
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?
Upvotes: 0
Views: 185
Reputation: 635
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
Upvotes: 1
Reputation: 5128
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.
Upvotes: 1