1

The following is the product class.

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; }
    }

I want to show the list of products on a page. But while showing price it is showing me a decimal value like 15.00 but I just want to show 15. This is my listofproducts.cshtml file

@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"); }

}

I just want to show the price in integer format. please help me.

2 Answers2

3

MVC offers many ways to set the format of a field. At least one option is to set the DisplayFormat attribute on the Price property with a format that specifies 0 fractional digits.

You can use the same standard or custom format strings you use with String.Format or Decimal.ToString. For example, N0 formats the decimal as a number with 0 fractional digits:

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

This attribute works only with the DisplayFor and EditFor methods.

Check the Standard Numeric Format Strings

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
1

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

just add the string format in razor view. i hope it should be help you thanks.

   @Html.TextBoxFor(m => m.Price,Model.Price.ToString("G29"))
Community
  • 1
  • 1
Lalji Dhameliya
  • 1,729
  • 1
  • 17
  • 26