如何在剃刀视图中以整数格式显示十进制值

时间:2016-12-16 11:44:21

标签: asp.net-mvc

以下是产品类别。

public partial class Product
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Product()
        {
            this.Carts = new HashSet<Cart>();
            this.OrderDetails = new HashSet<OrderDetail>();
        }

        [Key]
        public int ProductID { get; set; }
        [Required(ErrorMessage = "ProductName is required")]
        [RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
        [StringLength(200, ErrorMessage = " Name lenght should be 10 to 200 char", MinimumLength = 10)]
        public string ProductName { get; set; }
        [Required(ErrorMessage = "ProductImage is required")]
        public string ProductImage { get; set; }
        [Required(ErrorMessage = "Description is required")]
        [StringLength(200, ErrorMessage = " Description should be 100 to 2000 char", MinimumLength = 100)]
        public string Description { get; set; }
        [Required(ErrorMessage = "Price is required")]
        [RegularExpression("^[0-9]*$", ErrorMessage = "Price must be numeric")]
        public decimal Price { get; set; }
        [Key]
        [ForeignKey("ProductCategory")]
        public int CategoryID { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Cart> Carts { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<OrderDetail> OrderDetails { get; set; }
        public virtual ProductCategory ProductCategory { get; set; }
    }

我想在页面上显示产品列表。但在显示价格时,它显示的是十进制值,如15.00,但我只想显示15。 这是我的listofproducts.cshtml文件

@model PagedList.IPagedList<Shoppers.Context.Product>
@using PagedList.Mvc;
@if (@Session["Name"] != null)
{
    if (@Session["Name"].Equals("shopper.admin@yopmail.com"))
    {

        ViewBag.Title = "ListOfProducts";
        Layout = "~/Views/Shared/_Layout1.cshtml";
    }
    else
    {
        ViewBag.Title = "ListOfProducts";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    <div class="container">
        <div class="row">
            <div class="col-sm-3">
                <div class="admin-left-sidebar">
                    <h2>Action Menu</h2>
                    <div class="panel-group category-products" id="accordian">
                        <!--category-productsr-->
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h4 class="admin-panel-title">
                                    <a href="@Url.Action("ListOfProducts", "Product")" class="elements">
                                        <span>List Products</span>
                                    </a>
                                </h4>
                            </div>
                        </div>
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h4 class="admin-panel-title">
                                    <a href="@Url.Action("CreateNewProducts", "Product")" class="elements">
                                        <span>Add/update Product</span>
                                    </a>
                                </h4>
                            </div>
                        </div>
                    </div><!--/category-productsr-->
                </div>
            </div>
            <div class="col-sm-9 padding-right">
                <div class="features_items">
                    <h2 class="admin-title text-center">List Products</h2>
                    <div class="list-product-style pull-right">

                        <span class="product-category list-product-style">
                            @using (Html.BeginForm("ListOfProducts", "Product",FormMethod.Get))
                            {
                                @Html.TextBox("Search")<input type="submit" value="Search" />
                            }
                           @* <a class="list-product-table-link-style">Search</a>
    <span><input type="text" name="Category" class="list-product-name-style"></span>*@
                        </span>
                    </div>
                    <p>
                        @Html.ActionLink("Create New", "CreateNewProducts")
                    </p>
                    <table class="table table-bordered">
                        <tr class="table-background">
                            <th>
                                ProductName
                            </th>
                            <th>
                                ProductImage
                            </th>
                            <th>
                                Description
                            </th>
                            <th>
                                Price
                            </th>
                            <th>
                                Category Name
                            </th>
                            <th>
                                Action
                            </th>
                        </tr>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td>
                                    @Html.DisplayFor(modelItem => item.ProductName)
                                </td>
                                <td>
                                    <img src="@Url.Content(item.ProductImage)" style="width:40px;height:40px" alt="" />
                                </td>
                                <td>
                                    <textarea disabled> @Html.DisplayFor(modelItem => item.Description)</textarea>
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => item.Price)
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => item.ProductCategory.CategoryName)
                                </td>
                                <td>
                                    @Html.ActionLink("Edit", "UpdateTheProduct", new { id = item.ProductID }) |
                                    @Html.ActionLink("Details", "ReadProductDetails", new { id = item.ProductID }) |
                                    @using (Html.BeginForm("Delete", "Product", new { id = item.ProductID }))
                                    {
                                        <input type="submit" value="Delete" onclick="return confirm('Are you sure you want to delete record');" />
                                    }
                                </td>
                            </tr>
                        }
                    </table>
                    @Html.PagedListPager(Model, page => Url.Action("ListOfProducts", new { page, pageSize = Model.PageSize }))
                    @if (Model.TotalItemCount == 0)
                {
                        <p>There are no products!!!!</p>
                    }
                </div>
            </div>
        </div>
    </div>
}
else
{
    { Response.Redirect("~/Account/Login"); }

}

我只想以整数格式显示价格。请帮帮我。

2 个答案:

答案 0 :(得分:2)

MVC提供了许多设置字段格式的方法。至少有一个选项是使用指定0小数位数的格式在Price属性上设置DisplayFormat属性。

您可以使用与String.FormatDecimal.ToString相同的标准或自定义格式字符串。例如,N0将小数格式化为带0位数的数字:

[DisplayFormat(DataFormatString = "{0:N0}", ApplyFormatInEditMode = true)]
[Required(ErrorMessage = "Price is required")]
[RegularExpression("^[0-9]*$", ErrorMessage = "Price must be numeric")]
public decimal Price { get; set; }

此属性仅适用于DisplayForEditFor方法。

检查Standard Numeric Format Strings

答案 1 :(得分:1)

How do I format to only include decimal if there are any

类似

只需在razor视图中添加字符串格式即可。 我希望它应该对你有所帮助。

   @Html.TextBoxFor(m => m.Price,Model.Price.ToString("G29"))