问题
MVC RAZOR VIEW
Html.EditorFors decimal places can be controlled with Dataannotations but there is no easy (if any) way to control the width (they don't take htmlAttributes and setting a css class with a width has no effect).
Html.TextBoxFors allow easy control of the width with htmlAttributes but there is no easy (if any) way to control the decimal places.
How do you get an edit text box that you can control both the width and the decimal places?
回答1:
and setting a css class with a width has no effect
I suppose that's because you have an incorrect CSS selector.
You could do this for example:
<div class="myrule">
@Html.EditorFor(x => x.FooBar)
</div>
and then in your CSS file:
.myrule input {
width: 200px;
}
As an alternative you could write a custom editor template and then specify the name of this template when using the EditorFor call:
@Html.EditorFor(x => x.FooBar, "MyTemplate")
回答2:
use regular expressions to control the decimal places.
write a custom attribute inheriting System.ComponentModel.DataAnnotations.RegularExpressionAttribute which specify desired regular expression to control the decimal places.
then in the viewmodel/model specify the custom attribute to the desired field.
来源:https://stackoverflow.com/questions/13927811/how-to-control-both-the-width-and-decimal-places-for-a-text-box