我的ViewModel有一个自定义类型Money:
public class Money
{
public Money(decimal value)
{
Value = value;
}
public decimal Value { get; private set; }
public override string ToString()
{
return string.Format("{0:0.00}", Value);
}
}
我希望通过HTML.EditorFor(viewModel => viewModel.MyMoneyProperty)
在ASP.NET MVC中呈现一个文本框,但它不起作用。我必须在Money中实现一个特殊的界面吗?
提前致以最诚挚的问候和谢意,
斯特芬
答案 0 :(得分:3)
试试这样:
public class Money
{
public Money(decimal value)
{
Value = value;
}
[DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
public decimal Value { get; private set; }
}
并在您看来:
<%= Html.EditorFor(x => x.SomePropertyOfTypeMoney.Value) %>
或者您可以拥有Money(~/Views/Shared/EditorTemplates/Money.ascx
)的自定义编辑器模板:
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.Money>"
%>
<%= Html.TextBox("", Model.Value.ToString("0.00")) %>
然后在你看来:
<%= Html.EditorFor(x => x.SomePropertyOfTypeMoney) %>