MVC3 TextBoxFor vs EditorFor - problems with everyone

I browsed the website for a while, but cannot find a suitable solution for my specific problem. I am working on an MVC3 Razor application. I do a bunch of things on the client side using jQuery, so in this case I need input fields for the html Id attributes.

In this particular problem, when I use TextBoxFor to display the currency (decimal type), the value displays with 4 decimal places (I only need 2). I entered the model attribute

[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]

When it is displayed, it seems to ignore this "displayFormat". I saw other posts on the Internet confirming that this problem exists with the TextBoxFor html helper.

On the other hand, I can change the use of the EditorFor html helper, and the DisplayFormat model attribute works for formats of my text field value up to two decimal places. On the other hand, I am losing the ability to apply html attributes to my html helper, which I need for jQuery. What is the best way to get the best of both worlds? I saw a bunch of workarounds, but there seems to be an easier way to do this.

As a side note, my specific project requires that most of my fields have html Id attributes and sometimes classes, so I can do client-side jQuery operations. Not sure if this means that I should focus on extending the EditorFor html helper extension to also accept html attributes or not.

+4
source share
3 answers

DisplayFormat is only used by MVC in any of the t21 razor extension methods. (e.g. DisplayFor ). Unfortunately, to do what you want, you will have to write the code "manually" ... here is an example to get you started. It mainly reproduces the TextBoxFor code, but makes some assumptions, which are most likely bad for production. Check out the purpose of the displayText below, where we really care about the DisplayFormatString that you set in your DisplayFormat .

To call it in your view @Html.TextEditorFor(m => m.Value)

 public static class CustomEditorFor { public static IHtmlString TextEditorFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) { return TextEditorFor(helper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } public static IHtmlString TextEditorFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); object value = metadata.Model; string displayText = string.Format(metadata.DisplayFormatString, value); string fullName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); TagBuilder tagBuilder = new TagBuilder("input"); tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.MergeAttribute("type", "text"); tagBuilder.MergeAttribute("name", fullName, true); tagBuilder.MergeAttribute("value", displayText); tagBuilder.GenerateId(fullName); ModelState modelState; if (helper.ViewData.ModelState.TryGetValue(fullName, out modelState)) { if (modelState.Errors.Count > 0) { tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName); } } tagBuilder.MergeAttributes(helper.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata)); return new HtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing)); } } 
+2
source

The easy solution here is to create a custom display template and editor for your custom currency type. Do it.

 public class MyModel { [DataType(DataType.Currency)] public decimal mycurrency {get;set;} } 

Then in ~ / Views / Shared / EditorTemplates / Currency.cshtml (or DisplayTemplates)

 @model decimal? @Html.TextBox("", string.Format("{0:0.00}", Model), new { whateveryourattributes="attributevalues"}) 

Then you can just use DisplayFor or EditorFor or something else.

0
source

I ended up using Telerik Numeric Textbox. I am sure that both of the above answers will fix decimal formatting.

I still need to go back to model validation. I had problems with the EditorFor html helper, which checked the validity of the data annotations on the model for validation, doing the same with TextBoxFor. But at the same time, the flaw in the EditorFor helper did not accept the HtmlAttributes that I need for jQuery.

I started using MVC4 RC, so I hope that many of these things will be fixed in version 4.

0
source

Source: https://habr.com/ru/post/1414975/


All Articles