下面的代码工作正常,但在文本框中,十进制值的格式为“0,0000” (,是小数点分隔符)。我想只有2位小数。我怎么能这样做?
谢谢,
//Database model used with NHibernate
public class Bank
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName{ get; set; }
public virtual decimal Amount { get; set; }
}
//MVC Model
public class MyModel
{
public Bank Bank { get; set; }
}
//View
@Html.TextBoxFor(m => m.Bank.Amount, new { id = "tbAmount"})
在调试器中,我没有看到任何小数,我们一步一步地在里面(o @HTML.Textbofor)查看,该值没有任何小数但是当页面显示时有4位小数< / p>
//Database model used with NHibernate
public class Bank
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName{ get; set; }
public virtual decimal Amount { get; set; }
}
//Class for view
public class ViewBank
{
[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public decimal Amount { get; set; }
}
//MVC Model
public class MyModel
{
public Bank Bank { get; set; }
var ViewBank = new ViewBank() { Amount = Bank.Amount};
}
//View
@Html.TextBoxFor(m => m.Amount, new { id = "tbAmount"})
答案 0 :(得分:58)
我会使用编辑器模板,我会不在我的视图中使用我的NHibernate域模型。我将定义视图模型,这些模型是根据给定视图的要求专门定制的(在这种情况下将数量限制为2位小数):
[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public decimal Amount { get; set; }
然后:
@Html.EditorFor(m => m.Bank.Amount)
答案 1 :(得分:37)
这对我有用
@Html.TextBox("Amount", String.Format("{0:0.00}", Model.Bank.Amount), new { id = "tbAmount"})
修改强>
这适用于TextBoxFor(不适用于MVC3)
@{var formated = String.Format("{0:0.00}", Model.Bank.Amount);}
@Html.TextBoxFor(m => m.Bank.Amount, formated, new { id = "tbAmount"})
答案 2 :(得分:15)
在MVC 4中,您现在可以将格式作为第二个参数传递
final List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits));
// Create an ArrayAdapter from List
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, fruits_list){
@Override
public View getView(int position, View convertView, ViewGroup parent){
// Get the current item from ListView
View view = super.getView(position,convertView,parent);
if(position %2 == 1)
{
// Set a background color for ListView regular row/item
view.setBackgroundColor(Color.parseColor("#FFB6B546"));
}
else
{
// Set the background color for alternate row/item
view.setBackgroundColor(Color.parseColor("#FFCCCB4C"));
}
return view;
}
};
// DataBind ListView with items from ArrayAdapter
lv.setAdapter(arrayAdapter);
}
答案 3 :(得分:1)
如果您没有Decimal
类型的自定义编辑器模板,则EditorFor
装饰的DisplayFormatAttribute
可能会开箱即用。
对于自定义编辑器模板,我最终使用了以下内容:
@model decimal?
@{
string displayValue;
if (Model == null)
{
displayValue = null;
}
else {
var formatString = (ViewData.ModelMetadata).DisplayFormatString;
displayValue = formatString == null ? Model.ToString() : string.Format(formatString, Model);
}
}
<div class="form-group">
@Html.LabelFor(c => c)
@Html.TextBoxFor(c => c, new { type = "text", Value = displayValue, @class = "form-control" })
@Html.ValidationMessageFor(c => c)
</div>
当使用DisplayFormatAttribute
装饰属性时,该方法适用:
[DisplayFormat(DataFormatString = "{0:n1}", ApplyFormatInEditMode = true), Display(Name = "Commission")]
public decimal? CommissionPercentage { get; set; }
答案 4 :(得分:0)
这对我有用。
在 MVC5 中,在视图中:
@Html.TextBoxFor(o => o.Amount, "{0:n0}", new { @class = "form-control" })
StringFormatting 前的输出 --> 15000.00
StringFormatting 后的输出 --> 15000