MVC 2, display model model in a text box

I have a simple one for you guys. In my opinion, I have a text box, for example:

<%= Html.TextBoxFor(x => x.Price, new { @class = "text tiny" })%>

The price is decimal. When the form loads. the text field displays "0". I would like it to display "0.00".

I tried <%= Html.TextBoxFor(x => String.Format("{0:0.00}", x.Price), new { @class = "text tiny" })%>, which was wrong.

Templates can only be used with the access, property access, one-dimensional array index, or one-parameter custom expression index.

+3
source share
3 answers

You can use ModelMetadata . Some metadata attributes: EditFormatStringand DisplayFormatString.

+3

:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<decimal?>" %>
<%= Html.TextBox( string.Empty, (Model.HasValue ? Model.Value.ToString("C") : string.Empty), new { @class = "money" } ) %>

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<decimal?>" %>
<%= Html.TextBox( string.Empty, (Model.HasValue ? Model.Value.ToString("0.00") : string.Empty), new { @class = "money" } ) %>

CSS, , . Money.ascx, Views\Shared\DisplayTemplates Views\Shared\EditorTemplates .

<%= Html.DisplayFor( x => x.Price, "Money" ) %>
<%= Html.EditorFor( x => x.Price, "Money" ) %>

EDIT: , , / ( ), DataAnnotationsModelMetadataProvider, EditFormatAttribute, ( DataAnnotations), , .

public class ExtendedDataAnnotationsMetadataProvider : DataAnnotationsModelMetadataProvider
{
    private HttpContextBase Context { get; set; }

    public ExtendedDataAnnotationsMetadataProvider() : this( null ) { }

    public ExtendedDataAnnotationsMetadataProvider( HttpContextBase httpContext )
    {
        this.Context = httpContext ?? new HttpContextWrapper( HttpContext.Current );
    }
    protected override ModelMetadata CreateMetadata( IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName )
    {
        List<Attribute> attributeList = new List<Attribute>( attributes );
        var metadata = base.CreateMetadata( attributes, containerType, modelAccessor, modelType, propertyName );
        EditFormatAttribute editFormatAttribute = attributeList.OfType<EditFormatAttribute>().FirstOrDefault();
        if (editFormatAttribute != null)
        {
            metadata.EditFormatString = editFormatAttribute.EditFormatString;
        }

       // RequiredAdminAttribute requiredAdminAttribute = attributeList.OfType<RequiredAdminAttribute>().FirstOrDefault();
       // if (requiredAdminAttribute != null)
       // {
       //     metadata.IsRequired = this.Context.User == null || requiredAdminAttribute.RequiredForUser( this.Context.User );
       // }

        return metadata;
    }
}

public class EditFormatAttribute : Attribute
{
    public string EditFormatString { get; set; }
}

Global.asax.cs Application_Start()

ModelMetadataProviders.Current = new ExtendedDataAnnotationsMetadataProvider();

, :

[DataType( DataType.Currency )]
[DisplayFormat( DataFormatString = "{0:C}", ApplyFormatInEditMode = false )]
[EditFormat( EditFormatString = "{0:0.00}" )]
public decimal? Amount { get; set; }

, , HTML . , , . , , ( ).

+4

, , , , EditorFor TextBoxFor. EditFormatString ModelMetadata​​p >

+3

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


All Articles