尝试使用属性3 times
显示货币并使用[DataType(DataType.Currency)]
时,在本地调试时,我的OS X计算机上的格式无法正确显示。它显示为¤符号而不是$。
答案 0 :(得分:3)
您可以在Startup.cs
public Startup(IHostingEnvironment env)
{
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
答案 1 :(得分:0)
这是另一种方法:
型号:
namespace WebApplication1.Models
{
public class Employee
{
public decimal Salary { get; set; }
public string SalaryUS
{
get { return string.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", Salary); }
}
public string SalaryPL
{
get { return string.Format(new System.Globalization.CultureInfo("pl-PL"), "{0:C}", Salary); }
}
}
}
控制器:
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
var model = new Employee()
{
Salary = 1000
};
return View(model);
}
}
}
查看:
@model WebApplication1.Models.Employee
@Html.DisplayFor(model => model.SalaryUS) <br/>
@Html.DisplayFor(model => model.SalaryPL)
结果:
$1,000.00
1 000,00 zł