C# C中强类型视图的字符串格式
我想知道如何将强类型的视图价格字段转换为2位说明符,就像我的数据库中有一个货币字段,它将例如15转换为15.0000,我想在视图中显示15.00,下面是代码:C# C中强类型视图的字符串格式,c#,asp.net-mvc,asp.net-mvc-2,string-formatting,strongly-typed-view,C#,Asp.net Mvc,Asp.net Mvc 2,String Formatting,Strongly Typed View,我想知道如何将强类型的视图价格字段转换为2位说明符,就像我的数据库中有一个货币字段,它将例如15转换为15.0000,我想在视图中显示15.00,下面是代码: <%: Html.TextBoxFor(model =>model.Price, new { maxlength = "5", style = "width:40px;" })%> 我尝试了类似的方法,但没有成功: <%: Html.TextBoxFor(model => String.Format("{0
<%: Html.TextBoxFor(model =>model.Price, new { maxlength = "5", style = "width:40px;" })%>
我尝试了类似的方法,但没有成功:
<%: Html.TextBoxFor(model => String.Format("{0:n}"model.Price), new { maxlength = "5", style = "width:40px;" })%>
试试这个:
<%: Html.TextBoxFor(model => model.Price.ToString("0.00"), new { maxlength = "5", style = "width:40px;" })%>
更新:
您的原始语法中还漏掉了一个逗号,这可能也是阻止它以这种方式工作的原因。应该是:
<%: Html.TextBoxFor(model => String.Format("{0:n}", model.Price), new { maxlength = "5", style = "width:40px;" })%>
另外,对于小数点后2位,请按如下方式尝试:
<%: Html.TextBoxFor(model => String.Format("{0:0.00}", model.Price), new { maxlength = "5", style = "width:40px;" })%>
试试这个:
<%: Html.TextBoxFor(model => model.Price.ToString("0.00"), new { maxlength = "5", style = "width:40px;" })%>
更新:
您的原始语法中还漏掉了一个逗号,这可能也是阻止它以这种方式工作的原因。应该是:
<%: Html.TextBoxFor(model => String.Format("{0:n}", model.Price), new { maxlength = "5", style = "width:40px;" })%>
另外,对于小数点后2位,请按如下方式尝试:
<%: Html.TextBoxFor(model => String.Format("{0:0.00}", model.Price), new { maxlength = "5", style = "width:40px;" })%>
最好在视图模型上使用DataAnnotations显示格式属性。大概是这样的:
<%: Html.TextBoxFor(model => String.Format("{0:0.00}", model.Price), new { maxlength = "5", style = "width:40px;" })%>
[DisplayFormatDataFormatString={0:n}]
然后使用Html.EditorFormodel=>model.Price来呈现输入。最好在视图模型上使用DataAnnotations显示格式属性。大概是这样的:
<%: Html.TextBoxFor(model => String.Format("{0:0.00}", model.Price), new { maxlength = "5", style = "width:40px;" })%>
[DisplayFormatDataFormatString={0:n}]
然后使用Html.EditorFormodel=>model.Price来呈现输入。您可以在模型上添加如下属性:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:n}")]
如果不想这样做,则需要使用“旧”样式文本框:
<%= Html.TextBox("Price", string.Format("{0:n}", Model.Price)) %>
您可以在模型上放置一个属性,如下所示:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:n}")]
如果不想这样做,则需要使用“旧”样式文本框:
<%= Html.TextBox("Price", string.Format("{0:n}", Model.Price)) %>
您是否尝试过使用mode.Price.ToString并在ToString方法中指定所需的格式字符串?您是否尝试过使用mode.Price.ToString并在ToString方法中指定所需的格式字符串?这可能会有所帮助
private DateTime hDay;
[DisplayName("Hire Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime HireDate
{
get
{
if (hDay == DateTime.MinValue)
{
return DateTime.Today;
}
else
return hDay;
}
set
{
if (value == DateTime.MinValue)
{
hDay = DateTime.Now;
}
}
}
或
我们也可以用这种方法
@Html.TextBoxFor(m => m.MktEnquiryDetail.CallbackDate, "{0:dd/MM/yyyy}")
这可能会有所帮助
private DateTime hDay;
[DisplayName("Hire Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime HireDate
{
get
{
if (hDay == DateTime.MinValue)
{
return DateTime.Today;
}
else
return hDay;
}
set
{
if (value == DateTime.MinValue)
{
hDay = DateTime.Now;
}
}
}
或
我们也可以用这种方法
@Html.TextBoxFor(m => m.MktEnquiryDetail.CallbackDate, "{0:dd/MM/yyyy}")
已得到答复[此处][1]:。[1] :已得到答复[此处][1]:。[1]:方法“ToString”不重载1个参数-注意到您使用的TextBoxFor版本我认为不接受格式。方法“ToString”不重载1个参数-注意到您使用的TextBoxFor版本我认为不接受格式。错误:方法“ToString”不重载1个参数arguments@Mr我必须假设Price是一个十进制数,它有一个ToString和format参数。它是什么类型?错误:“ToString”方法没有重载1arguments@Mr我必须假设Price是一个十进制数,它有一个ToString和format参数。它是什么类型的?如果我使用它,那么我不能更新,因为强类型视图会自动更新如果我使用它,那么我不能更新,因为强类型视图会自动更新