2
  • How to specify MaxLength for a textbox , Like MaxLenth="18"

  • How to Set the TextBox Format String as FormatString="$###,###,###,##0.00" in model so even if i enter 100 it should automatically become $100.00

1 Answer 1

6

How to specify MaxLength for a textbox , Like MaxLenth="18"

You could pass additional html attributes to the TextBoxFor method:

<%= Html.TextBoxFor(x => x.SomeValue, new { maxlength = "18" })

How to Set the TextBox Format String as FormatString="$###,###,###,##0.00" in model so even if i enter 100 it should automatically become $100.00

You could use the [DisplayFormat] attribute:

[DisplayFormat(DataFormatString = "{0:$###,###,###,##0.00}", ApplyFormatInEditMode = true)]
public decimal? Value { get; set; }

and then:

<%= Html.EditorFor(x => x.Value) %>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the maxlength.But [DisplayFormat(DataFormatString = "{0:$###,###,###,##0.00}", ApplyFormatInEditMode = true)] public decimal? Value { get; set; } is not working , its not at all formating as desired.
@Basavaraj, did you use Html.EditorFor(x => x.Value) in your view instead of Html.TextBoxFor(x => x.Value)? The DisplayFormat attribute applies only when using the EditorFor helper.

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.