How to control both the width and decimal places for a text box?

我们两清 提交于 2019-12-25 01:00:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!