在MVC Core
上的我的ASP.NET VS2015
网络项目中,以下模型显示数据,例如15481而不是15,481美元,即使我在下面使用[DisplayFormat]
:< / p>
模型:
public class State
{
[Key]
public int StateId { get; set; }
[Column(TypeName ="varchar(40)")]
public string StateName { get; set; }
[Column(TypeName = "char(2)")]
public string StateCode { get; set; }
}
public class Sales
{
[Key]
public int SalesId { get; set; }
public int? FiscalYear { get; set; }
[DisplayFormat(DataFormatString = "{(0:C0)}")]
public float? SaleAmount { get; set; }
public int StateId { get; set; }
public State State { get; set; }
}
模型视图:
public class StatesSalesViewModel
{
[HiddenInput]
public int StateId { get; set; }
[Display(Name ="State")]
public string StateName { get; set; }
public int? FiscalYear { get; set; }
[DisplayFormat(DataFormatString = "{(0:C0)}")]
public float? SaleAmount { get; set; }
}
控制器:
public async Task<IActionResult> FYSales(List<StatesSalesViewModel> model, string GO, int currentlySelectedIndex, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
ViewBag.YearsList = Enumerable.Range(1996, 29).Select(g => new SelectListItem { Value = g.ToString(), Text = g.ToString() }).ToList();
if (!string.IsNullOrEmpty(GO))
{
var qryVM = from s in _context.States
join g in _context.Sales on s.StateId equals g.StateId
where g.FiscalYear == currentlySelectedIndex
select new StatesSalesViewModel() {StateId = s.StateId, StateName = s.StateName, SaleAmount = g.SaleAmount , FiscalYear = currentlySelectedIndex };
return View(qryVM.ToList());
}
}
查看:
@model IList<mProject.Models.StatesSalesViewModel>
<div class="row">
<div class="col-md-12">
<form asp-controller="StatesSales" asp-action="getSales" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post">
@{
IEnumerable<SelectListItem> yearsList = (IEnumerable<SelectListItem>)ViewBag.YearsList;
var currentlySelectedIndex = 0; // Currently selected index (usually will come from model)
}
<strong>Select a Post Year</strong>
<h6>Choose a year o begin:</h6>
<label>Year:</label><select asp-for="@currentlySelectedIndex" asp-items="yearsList"></select><input type="submit" class="btn btn-default" name="GO" value="GO" />
<table class="table">
<thead>
<tr>
<th></th>
<th></th>
<th>Fiscal Year</th>
<th>State</th>
<th>Sales</th>
</tr>
</thead>
<tbody>
@for (int i=0; i< Model.Count(); i++)
{
<tr>
<td>@Html.HiddenFor(r => r[i].StateID)</td>
<td>@Html.HiddenFor(r => r[i].FYSalesID)</td>
<td>
@Html.TextBoxFor(r => r[i].FiscalYear)
</td>
<td>
@Html.TextBoxFor(r => r[i].StateName)
</td>
<td>
@Html.TextBoxFor(r => r[i].SaleAmount)
</td>
</tr>
}
</tbody>
</table>
<button type="submit" class="btn btn-default">Save</button>
</form>
</div>
</div>
答案 0 :(得分:3)
使用[DisplayFormat]
或@Html.DisplayFor()
时,才会尊重@Html.EditorFor()
属性。使用TextBoxFor()
时会忽略它。
此外,如果您想将其与@Html.EditorFor(r => r[i].SaleAmount)
一起使用,则需要修改该属性以包含ApplyFormatInEditMode属性
[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
public float? SaleAmount { get; set; }
然而,这对你来说没用,因为虽然它会正确地显示在文本框中,但它不会绑定回你float
属性,除非你还要创建一个转换的自定义模型绑定器(比如说) )"$15,481"
返回float
答案 1 :(得分:0)
可以使用货币注释。然而,它只是告诉MVC使用哪个显示或编辑器模板。正如我们所说,当前模板使用系统货币。您必须提供自定义编辑器模板或显示模板以及其他一些方法来确定要显示的货币符号。Look here如何提供您自己的实现
尝试使用此
[DisplayFormat(DataFormatString = "{0:C0}")]
示例
public class Sales
{
[Key]
public int SalesId { get; set; }
[DisplayFormat(DataFormatString = "{0:C0}")]
public float? SaleAmount { get; set; }
}
Check here了解更多详情