Professional Documents
Culture Documents
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
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
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
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
4 Tạo Movie
Chuột phải lên thư mục Models, chọn Add -> Class
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; }
[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; }
[NotMapped]
[Display(Name = "Hình ảnh")]
public HttpPostedFileBase PictureUpload { get; set; }
[ForeignKey("GenreObj")]
public int? GenreID { 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 ý
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
[Key]
public int ID { get; set; }
[Display(Name = "Tên")]
[Required]
[StringLength(200)]
public string Name { get; set; }
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
tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn
DateTime dt = (DateTime)value;
return ValidationResult.Success;
return new ValidationResult(ErrorMessage ?? "Dữ liệu ngày phải lớn hơn ngày
hôm nay");
tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn
Sau khi tạo GenreAttribute thì vẫn còn 1 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.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:
tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn
Enable-Migrations
add-migration Movie
tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn
update-database
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
Trong mục Add Scaffold, chọn MVC5 Controller with views, using Entity
Framework
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
Chạy F5 và link tới …/Genres, click vào Create và tạo ra 1 thể loại mới
[HttpPost]
[ValidateAntiForgeryToken]
tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn
[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");
}
Thành
@using (Html.BeginForm("Create",
"Movies",
FormMethod.Post,
new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form"
}))
Và
<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
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
tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn
Thành
<img src="data:image;base64,@System.Convert.ToBase64String(item.Picture)"
style="width: 200px;" />
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
<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
tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn
[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; }
update-database -force
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
{
chiTietHoaDon.SoLuong++;
}
db.SaveChanges();
return View(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>
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);
}
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 = "Email")]
[Required]
[EmailAddress]
public string Email { get; set; }
[Display(Name = "Mobile")]
[Required]
public int Mobile { get; set; }
@{
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
<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>
}
tuannm@ueh.edu.vn
Phát triển ứng dụng Web – Bit.ueh.edu.vn – tuannm@ueh.edu.vn
//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
[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());
}
}
<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>
@{
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>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
Thành
<footer>
<p>© @DateTime.Now.Year – MSSV – Họ và Tên</p>
</footer>
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.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
tuannm@ueh.edu.vn