Tuesday, August 27, 2019
In the previous post we successfully used the repository pattern to retrieve products from the database and set them to the Product objects.
3. We are ready to display some data in the Index.cshtml page first we are just going to make sure that everything is working by just typing in the following code
When you run the application you should see that the data is coming through and we can display the object in the Index.cshtml page.
4. Now let's use a little bootstrap to spruce it up a little with following markup
The code above lists the products in rows of four columns it format the price with "c" to give it a $ in front of it. The .toString("c") formats the decimal number to the locale of user so if a user is from England he would have the pound symbol. I've also added a "Buy" button that we will implement later.
The final page should look like this.
The entire markup for the Index.cshtml page is the following:
Previous: Hour 15 AspNet.Core Use Repository Pattern To Show Poducts
Next: Hour 17: ASP.NET Core Add log4Net Support To NorthwindCafe
In this post we are going to display those products in the Index.cshtml page
Here are the steps:
1. Open the Index.cshtml page in the Views/Home folder
2. Now declare a model of enumerable of products at the top of the page like this
@model IEnumerable<Product>
You will noticed that the Product class has a red underline on it, when you mouse over Visual Studio will say that the Product reference is missing
To fix this issue we have to add the Product namespace, which is NorthwindCafe.Web.Models in the _ViewImports.cshtml page. Add the following line to the _ViewImports.cshtml file
@using NorthwindCafe.Web.Models
Your _ViewImports.cshtml file should look like the following, up to this point
@addTagHelper "*,Microsoft.AspNetCore.Mvc.TagHelpers"@using NorthwindCafe.Web.Models
3. We are ready to display some data in the Index.cshtml page first we are just going to make sure that everything is working by just typing in the following code
@foreach(var p in Model)
{
@p.Name<br>
@p.Description<br>
@p.Price<br>
}
When you run the application you should see that the data is coming through and we can display the object in the Index.cshtml page.
4. Now let's use a little bootstrap to spruce it up a little with following markup
<div class="row">@foreach (var p in Model){<div class="col-md-4" style="margin-top:20px"><div class="row"><div class="col-md-4 text-right"><strong>Product Name:</strong></div><div class="col-md-4 text-left"> @p.Name</div></div><div class="row"><div class="col-md-4 text-right"><strong>Description:</strong></div><div class="col-md-4">@p.Description</div></div><div class="row"><div class="col-md-4 text-right"><strong>Price:</strong></div><div class="col-md-4">@p.Price.ToString("c")</div></div><div class="row"><div class="col-md-7 text-right"><button class="btn btn-primary">Buy</button></div></div></div>}</div>
The code above lists the products in rows of four columns it format the price with "c" to give it a $ in front of it. The .toString("c") formats the decimal number to the locale of user so if a user is from England he would have the pound symbol. I've also added a "Buy" button that we will implement later.
The final page should look like this.
The entire markup for the Index.cshtml page is the following:
@model IEnumerable<Product><div class="row">@foreach (var p in Model){<div class="col-md-4" style="margin-top:20px"><div class="row"><div class="col-md-4 text-right"><strong>Product Name:</strong></div><div class="col-md-4 text-left"> @p.Name</div></div><div class="row"><div class="col-md-4 text-right"><strong>Description:</strong></div><div class="col-md-4">@p.Description</div></div><div class="row"><div class="col-md-4 text-right"><strong>Price:</strong></div><div class="col-md-4">@p.Price.ToString("c")</div></div><div class="row"><div class="col-md-7 text-right"><button class="btn btn-primary">Buy</button></div></div></div>}</div>
Previous: Hour 15 AspNet.Core Use Repository Pattern To Show Poducts
Next: Hour 17: ASP.NET Core Add log4Net Support To NorthwindCafe
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
I have been working on this tutorial, however I am still getting an error on Product class having references even after updating _ViewImports.cshtml, I have been debugging the application for over a week with no success. What is it that I do not do right?
ReplyDeleteĐặt vé máy bay tại Aivivu, tham khảo
ReplyDeleteve may bay di my gia re
vé máy bay từ california về việt nam
vé máy bay từ đức về việt nam giá rẻ
vé máy bay giá rẻ từ nga về việt nam
chuyến bay từ anh về việt nam
lịch bay từ pháp về việt nam
chuyen bay chuyen gia