I've made a C# ASP.NET with Entity Framework, Repository Pattern and SQL. I want to retrieve two kind of numbers (Price & Fuel).
Price example: I want to retrieve 2,441.095, but it shows as 2441095,000 in SQL and 2441095,00 in ASP.NET. I have used decimal(18, 3).
Fuel example: I want to retrieve 22.0, but it shows as 22.00 in ASP.NET. I use decimal(18, 1) for that.
From the controller class:
// GET: Car/Details/5
public ActionResult Details(int id)
{
var objCar = iCar.GetCarByID(id);
var Car = new Car();
Car.Id = id;
Car.Name = objCar.Name;
Car.Model = objCar.Model;
Car.Year = objCar.Year;
Car.Color = objCar.Color;
Car.Fuel = objCar.Fuel;
Car.Price = objCar.Price;
return View(Car);
}
From the Repository Pattern:
public Car GetCarByID(int Id)
{
return dbContext.Cars.Find(Id);
}
Razor:
@model IEnumerable<Car_Sales.Models.Car>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Model)
</th>
<th>
@Html.DisplayNameFor(model => model.Year)
</th>
<th>
@Html.DisplayNameFor(model => model.Color)
</th>
<th>
@Html.DisplayNameFor(model => model.Fuel)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model)
</td>
<td>
@Html.DisplayFor(modelItem => item.Year)
</td>
<td>
@Html.DisplayFor(modelItem => item.Color)
</td>
<td>
@Html.DisplayFor(modelItem => item.Fuel)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>