You are on page 1of 49

Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.

vn

Hướng dẫn lẫm Website bẫng ASP MVC


tuannm@ueh.edu.vn

Lưu ý: các bước trong bài hướng dẫn sẽ có mối quan hệ với nhau nên phải hoàn thành
bước trước thì mới đến làm bước sau để tránh những lỗi khó hiểu

1 Tạo dự án ASP MVC

1.1 Đặt tên dự án theo cấu trúc: MSSV_HoVaTen => 5111111111_NguyenVanA

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

1.2 Chọn MVC và chọn “Change Authentication” => “Individual User Accounts”

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

1.3 Kết quả cấu trúc dự án được tạo ra

2 Tạo CSDL trong somee.com


2.1 Làm theo hướng dẫn từ link sau để đăng ký:
https://huynhthaihung.com/c-sharpc/huong-dan-dua-website-asp-net-len-hosting-mien-
phi-somee-com.html

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

2.2 Tạo cơ sở dữ liệu

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

2.3 Copy connection string từ CSDL mới tạo ra

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

3 Kết nối CSDL và ASP MVC


Mở Web.config file và kiếm “connectionStrings” section và thay thế connection string
với connection string ở bước trên:

Nhấn F5 để chạy chương trình từ Visual Studio và đăng ký mới 1 user

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

3.1 Sử dụng Server Explorer để kết nối đến CSDL


Chọn View/Server Explorer, sau đó nhấn chuột phải lên Data Connections và nhấn nút
Refresh

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

4 Tạo Movie
Chuột phải lên thư mục Models, chọn Add -> Class

Đặt tên lớp là Movie với nội dung:

public class Movie


{
public int ID { get; set; }

[Display(Name = "Tiêu Đề")]

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

[Required]
[StringLength(200, MinimumLength = 3)]
public string Title { get; set; }

[Display(Name = "Tóm tắc")]


[StringLength(int.MaxValue, MinimumLength = 3)]
public string Summary { get; set; }

[Display(Name = "Ngày phát hành")]


[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode
= true)]
//"{0:yyyy-MM-dd}"
[CheckDateGreaterThanTodayAttribute]
public DateTime ReleaseDate { get; set; }

[StringLength(10)]
[Display(Name = "Thể loại")]
[GenreAttribute]
public string Genre { get; set; }

[Display(Name = "Giá")]
[Range(5000, double.MaxValue)]
[DisplayFormat(DataFormatString = "{0:0,000}")]
public decimal Price { get; set; }

[Display(Name = "Xếp hạng")]


[Range(1, 5)]
public double Rated { get; set; }

public byte[] Picture { get; set; }

[NotMapped]
[Display(Name = "Hình ảnh")]
public HttpPostedFileBase PictureUpload { get; set; }

[ForeignKey("GenreObj")]
public int? GenreID { get; set; }

public virtual Genre GenreObj { get; set; }

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

Với những chỗ bị báo lỗi thì chọn using theo gợi ý

Sau khi using thì sẽ còn 2 lỗi như bên dưới

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

5 Tạo mới Genre class trong Model

public class Genre


{

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

[Key]
public int ID { get; set; }

[Display(Name = "Tên")]
[Required]
[StringLength(200)]
public string Name { get; set; }

public virtual IList<Movie> Movies { get; set; }


}

Tạo mới 1 thư mục trong project đặt tên là Validation

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

5.1 Nhấn chuột phải vào thư mục Validation mới tạo và tạo thêm 2 class như bên dưới

public class CheckDateGreaterThanTodayAttribute: ValidationAttribute

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

protected override ValidationResult IsValid(object value, ValidationContext


validationContext)

DateTime dt = (DateTime)value;

if (dt >= DateTime.UtcNow)

return ValidationResult.Success;

return new ValidationResult(ErrorMessage ?? "Dữ liệu ngày phải lớn hơn ngày
hôm nay");

5.2 Dùng using để fix lỗi compile

Tương tự cho GenreAttribute

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

public class GenreAttribute : ValidationAttribute


{
protected override ValidationResult IsValid(object value, ValidationContext
validationContext)
{
int genreID = int.Parse(value.ToString());
var db = new ApplicationDbContext();
if (db.Genres.Any(x => x.ID == genreID))
{
return ValidationResult.Success;
}
return new ValidationResult(
ErrorMessage ?? "Genre khong ton tai");
}

Sau khi tạo GenreAttribute thì vẫn còn 1 lỗi như bên dưới

5.3 Mở file Models/IdentityModels.cs và thêm 2 dòng sau dưới class


ApplicationDbContext:
public DbSet<Movie> Movies { get; set; }
public DbSet<Genre> Genres { get; set; }

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

Mở lại Model/Movie.cs và dùng using để fix lỗi

5.4 Nhấn F5 để chạy lại chương trình và đảm bảo khi bị lỗi và sẽ thấy kết quả như sau:

Dừng chương trình lại và chọn Tools/Nuget Package Manager/..Console

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

Enable-Migrations

Sau đó chạy tiếp:

add-migration Movie

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

Sẽ có 1 file được tạo ra trong thư mục Migrations

Sau đó chạy tiếp:

update-database

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

Kiểm tra lại CSDL đã xuất hiện 2 table mới

F5 chạy lại trang Web và đảm bảo không còn lỗi

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

6 Dừng và tạo dữ liệu Model từ Controller


 Chuột phải lên thư mục Controllers -> Controller để thêm 1 Controller mới.

 Trong mục Add Scaffold, chọn MVC5 Controller with views, using Entity
Framework

 Tên lớp là MoviesController, mô hình lớp Movie (Models), chọn


ApplicationDbContext cho lớp Context

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

6.1 Thay đổi Route mặc định là MoviesController


Mở file App_Start/RouteConfig.cs và thay đổi:

controller = "Home" => controller = "Movies"

F5 để chạy lại trang Web

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

7 Tương tự tạo Controller cho Genre

Chạy F5 và link tới …/Genres, click vào Create và tạo ra 1 thể loại mới

8 Lưu hình ảnh vào CSDL


8.1 Mở Controller/MoviesController.cs
Thay thế:

[HttpPost]
[ValidateAntiForgeryToken]

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

public ActionResult Create([Bind(Include =


"ID,Title,Summary,ReleaseDate,Genre,Price,Rated,Picture,GenreID")] Movie movie)

Với nội dung

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Movie movie)
{
if (ModelState.IsValid)
{
movie.Picture = new byte[movie.PictureUpload.ContentLength];
movie.PictureUpload.InputStream.Read(movie.Picture, 0,
movie.PictureUpload.ContentLength);

db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}

ViewBag.GenreID = new SelectList(db.Genres, "ID", "Name", movie.GenreID);


return View(movie);
}

8.2 Sau đó mở View/Movies/Create.cshtml và thay thế


@using (Html.BeginForm())

Thành

@using (Html.BeginForm("Create",
"Movies",
FormMethod.Post,
new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form"
}))

<div class="form-group">
@Html.LabelFor(model => model.Picture, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

@Html.EditorFor(model => model.Picture, new { htmlAttributes = new {


@class = "form-control" } })
@Html.ValidationMessageFor(model => model.Picture, "", new { @class =
"text-danger" })
</div>
</div>

Thành

<div class="form-group">
@Html.LabelFor(model => model.PictureUpload, htmlAttributes: new { @class
= "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.PictureUpload, new { type = "file",
@class = "form-control" })
@Html.ValidationMessageFor(model => model.Picture, "", new { @class =
"text-danger" })
</div>
</div>

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

8.3 F5 chạy chương trình và tạo mới 1 Movie

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

8.4 Sau đó mở View/Movies/Index.cshtml và thay thế


@Html.DisplayFor(modelItem => item.Picture)

Thành

<img src="data:image;base64,@System.Convert.ToBase64String(item.Picture)"
style="width: 200px;" />

8.5 Sau đó Refresh lại trang Web sẽ có nội dung sau

8.6 Sau đó mở View/Movies/Index.cshtml và thay thế toàn bộ với nội dung sau
@model IEnumerable<MSSV_NguyenVanA.Models.Movie>

@{
ViewBag.Title = "Index";

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

<h2>MSSV-Nguyen Van A</h2>

<div class="container">
<div class="row grid-demo">
@foreach (var item in Model)
{
<div class="col-md-4">
<img
src="data:image;base64,@System.Convert.ToBase64String(item.Picture)" style="width:
200px;" />
<p>@Html.DisplayNameFor(model => model.Title):
@Html.DisplayFor(modelItem => item.Title)</p>
<p>@Html.DisplayNameFor(model => model.Rated):
@Html.DisplayFor(modelItem => item.Rated)</p>
<p>@Html.DisplayNameFor(model => model.Price):
@Html.DisplayFor(modelItem => item.Price)</p>
<p>@Html.DisplayNameFor(model => model.Summary):
@Html.DisplayFor(modelItem => item.Summary)</p>
<p>@Html.DisplayNameFor(model => model.ReleaseDate):
@item.ReleaseDate.ToString("dd/MM/yyyy")</p>
<p>@Html.DisplayNameFor(model => model.Genre):
@Html.DisplayFor(modelItem => item.GenreObj.Name)</p>

@Html.ActionLink("Mua", "AddToCart",
new { id = item.ID })
</div>
}
</div>
</div>

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

9 Xây dựng Shopping Cart


9.1 Tạo các class sau trong thư mục Model:

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

public class ChiTietHoaDon


{
[Key]
public int MaChiTietHoaDon { get; set; }

[ForeignKey("HoaDonObj")]
public int MaHoaDon { get; set; }
public virtual HoaDon HoaDonObj { get; set; }

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

[ForeignKey("MovieObj")]
public int MaMovie { get; set; }
public virtual Movie MovieObj { get; set; }

public int SoLuong { get; set; }


}

9.2 Chọn Tools/Nuget Package Manager/..Console


 add-migration HoaDon –force

 update-database -force

9.3 Mở file Model/IdentityModel.cs và thêm thông tin sau:


public DbSet<HoaDon> HoaDons { get; set; }
public DbSet<ChiTietHoaDon> ChiTietHoaDons { get; set; }

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

Mở file Controller/MoviesController.cs và add thêm method bên dưới:

public ActionResult AddToCart(int id)


{
//Kiem tra Id movie ton tai hay khong
var movie = db.Movies.Where(x => x.ID == id).FirstOrDefault();
if (movie == null)
{
return RedirectToAction("Index");
}
var hoaDon = this.Session["HoaDon"] as HoaDon;
if (hoaDon == null)
{
hoaDon = new HoaDon();
hoaDon.NgayLap = DateTime.Now;
hoaDon.ChiTietHoaDons = new List<ChiTietHoaDon>();
this.Session["HoaDon"] = hoaDon;
db.HoaDons.Add(hoaDon);
}

//Kiem tra don hang da co truoc do


var chiTietHoaDon = hoaDon.ChiTietHoaDons.Where(x => x.MovieObj.ID ==
id).FirstOrDefault();
if (chiTietHoaDon == null)
{
chiTietHoaDon = new ChiTietHoaDon();
chiTietHoaDon.MaMovie = id;
chiTietHoaDon.MovieObj = movie;
chiTietHoaDon.HoaDonObj = hoaDon;
chiTietHoaDon.SoLuong = 1;
hoaDon.ChiTietHoaDons.Add(chiTietHoaDon);
}
else

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

{
chiTietHoaDon.SoLuong++;
}

db.SaveChanges();
return View(hoaDon);
}

9.4 Tạo mới file View/Movie/AddToCart.cshtml và điền thông tin


@model MSSV_NguyenVanA.Models.HoaDon

@{
ViewBag.Title = "Giỏ hàng";
}

<h2>Your cart</h2>
<table id="cartTable" class="table">
<thead>
<tr>
<th>Số lượng</th>
<th>Sản phẩm</th>
<th class="text-right">Giá</th>
<th class="text-right">Thành Tiền</th>
</tr>
</thead>
<tbody>
@foreach (var hoaDonChiTiet in Model.ChiTietHoaDons) {
<tr>
<td class="text-center">@hoaDonChiTiet.SoLuong</td>
<td class="text-left">@hoaDonChiTiet.MovieObj.Title</td>
<td class="text-right">@hoaDonChiTiet.MovieObj.Price.ToString("c")</td>
<td class="text-right">
@((hoaDonChiTiet.SoLuong *
hoaDonChiTiet.MovieObj.Price).ToString("c"))
</td>
<td>
@Html.ActionLink("Remove", "RemoveFromCart",
new { maMovies = hoaDonChiTiet.MaMovie }, new { @class = "btn btn-sm
btn-warning" })
</td>
</tr>

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

}
</tbody>
<tfoot>
<tr>
<td colspan="3" class="text-right">Total:</td>
<td class="text-right">
@Model.TongTien.ToString("c")
</td>
</tr>
</tfoot>
</table>
<div class="text-center">
@Html.ActionLink("Continue shopping", "Index", null, new { @class = "btn btn-
primary" })
@Html.ActionLink("Checkout now", "Checkout", null, new { @class = "btn btn-
primary" })
</div>

9.5 F5 chạy lại chương trình và click vào nút mua:

9.6 Tương tự xử lý cho Xóa khỏi giỏ hàng


public ActionResult RemoveFromCart(int maMovies)
{
var hoaDon = this.Session["HoaDon"] as HoaDon;
var chiTietHoaDon = hoaDon.ChiTietHoaDons.Where(x => x.MovieObj.ID ==
maMovies).FirstOrDefault();
hoaDon.ChiTietHoaDons.Remove(chiTietHoaDon);
return View("AddToCart", hoaDon);
}

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

9.7 Và xử lý Summary
public PartialViewResult Summary()
{
var hoaDon = this.Session["HoaDon"] as HoaDon;
if (hoaDon == null)
{
return null;
}
return PartialView(hoaDon);
}

9.8 Mở View/Share/_LoginPartial.cshtml và thêm


<ul class="nav navbar-nav navbar-right">
@Html.Action("Summary", "Movies")

10 Xử lý checkout
10.1 Tạo mới Model/ShippingDetail.cs với nội dung
public class ShippingDetail
{
public int ID { get; set; }

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

[Display(Name = "Ten")]
[Required]
public string Name { get; set; }

[Display(Name = "Dia Chi")]


[Required]
public string Address { get; set; }

[Display(Name = "Email")]
[Required]
[EmailAddress]
public string Email { get; set; }

[Display(Name = "Mobile")]
[Required]
public int Mobile { get; set; }

[Display(Name = "Ngày giao")]


[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode
= true)]
public DateTime ReleaseDate { get; set; }
}

10.2 Tạo mới View/Movies/Checkout.cshtml với nội dung


@model MSSV_NguyenVanA.Models.ShippingDetail

@{
ViewBag.Title = "Checkout";
}
<h2>Check out now</h2>
<p>Please enter your details, and we'll ship your goods right away!</p>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Movie</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

@Html.LabelFor(model => model.Name, htmlAttributes: new { @class =


"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new {
@class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class =
"text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new {
@class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class =
"text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new {
@class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class =
"text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Mobile, new { htmlAttributes = new {
@class = "form-control" } })
@Html.ValidationMessageFor(model => model.Mobile, "", new { @class =
"text-danger" })
</div>
</div>

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Hoàn thành" class="btn btn-default" />
</div>
</div>
</div>
}

10.3 Tạo mới 1 class EmailServiceNew với nội dung sau:


public class EmailServiceNew : IIdentityMessageService
{
public static async Task SendEmail(IdentityMessage message, string attachedFile =
null)
{
// Plug in your email service here to send an email.
await configSendGridasync(message, attachedFile);
}

public async Task SendAsync(IdentityMessage message)


{
// Plug in your email service here to send an email.
await configSendGridasync(message);
}

private static async Task configSendGridasync(IdentityMessage message, string


attachedFile = null)
{
try
{
//Debug.WriteLine("Send Email:" + message.Body);
//return;
string fromEmail = "xyz@gmail.com";
string fromPassword = "aaa";

var fromAddress = new MailAddress("CRM@gmail.com", "Ban hang BIS -


UEH");
var toAddress = new MailAddress(message.Destination, message.Destination);

string subject = message.Subject;


string body = message.Body;

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

string html = message.Body;


var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromEmail, fromPassword)
};
var messageSend = new MailMessage(fromAddress, toAddress);
messageSend.Subject = subject;

//messageSend.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text
, null, MediaTypeNames.Text.Plain));

messageSend.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html,
null, MediaTypeNames.Text.Html));
if (!string.IsNullOrEmpty(attachedFile))
{
Attachment data = new Attachment(
attachedFile,
MediaTypeNames.Application.Octet);
messageSend.Attachments.Add(data);
}
smtp.Send(messageSend);
}
finally
{
}
}
}

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

10.4 Setup gmail to send email


 Go to the "Less secure apps" section in My Account.

 Next to "Access for less secure apps," select Turn on

10.5 Add method Checkout trong MoviesController.cs


public ActionResult Checkout()
{
return View(new ShippingDetail());
}

[HttpPost]
public ActionResult Checkout(ShippingDetail detail)
{
var hoaDon = this.Session["HoaDon"] as HoaDon;
if (hoaDon.ChiTietHoaDons.Count() == 0)
{
ModelState.AddModelError("", "Sorry, your cart is empty!");
}
if (ModelState.IsValid)
{
StringBuilder body = new StringBuilder()
.AppendLine("A new order has been submitted")
.AppendLine("---")
.AppendLine("Items:");
foreach (var hoaDonChiTiet in hoaDon.ChiTietHoaDons)
{
var subtotal = hoaDonChiTiet.MovieObj.Price * hoaDonChiTiet.SoLuong;
body.AppendFormat("{0} x {1} (subtotal: {2:c}", hoaDonChiTiet.SoLuong,
hoaDonChiTiet.MovieObj.Title,
subtotal);
}
body.AppendFormat("Total order value: {0:c}", hoaDon.TongTien)
.AppendLine("---")
.AppendLine("Ship to:")
.AppendLine(detail.Name)
.AppendLine(detail.Address)
.AppendLine(detail.Mobile.ToString());

EmailServiceNew.SendEmail(new IdentityMessage()

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

{
Destination = detail.Email,
Subject = "New order submitted!",
Body = body.ToString()
});

this.Session["HoaDon"] = null;
return View("CheckoutCompleted");
}
else
{
return View(new ShippingDetail());
}
}

10.6 Add View/Movies/Summary.cshtml với nội dung sau:


@model MSSV_NguyenVanA.Models.HoaDon

<div class="navbar-right">
@Html.ActionLink("Checkout", "Checkout", "Movies",
new { @class = "btn btn-default navbar-btn" })
</div>
<div class="navbar-text navbar-right">
<b>Giỏ hàng của bạn:</b>
@Model.ChiTietHoaDons.Sum(x => x.SoLuong) sản phẩm,
@Model.TongTien.ToString("c")
</div>

Tạo mới View/Movies/CheckoutCompleted.cshtml với nội dung sau:

@{
ViewBag.Title = "Order Submitted";
}
<h2>Thanks!</h2>
Thanks for placing your order. We'll ship your goods as soon as possible.

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

10.7 F5 chạy lại trang Web, tiến hành mua và thanh toán đơn hàng

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

10.8 Mở file _Layout.cshtml trang trí lại theo sở thích cá nhân và đổi nội dung
<footer>
<p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
</footer>

Thành

<footer>
<p>&copy; @DateTime.Now.Year – MSSV – Họ và Tên</p>
</footer>

11 Publish trang Web lên Somee

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

11.1 Và nhấn nút Publish

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

11.2 Sau đó vào MSSV_NguyenVanA\bin\Release\Publish và zip tất cả thành


publish.zip

11.3 Mở Somee

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn

11.4 Chạy trang Web trên Somee

tuannm@ueh.edu.vn

You might also like