:
<%@ 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;
}
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 . , , . , , ( ).