Thêm thuộc tính Priority cho lớp Task

Một phần của tài liệu Tạo ứng dụng ASP mvc (Trang 52 - 55)

Thông thường bạn sẽ có rất nhiều tác vụ để thực hiện, tuy nhiên mỗi tác vụ có một mức độ quan trọng khác nhau, nên chúng ta cần phải cập nhật cho lớp Task để ứng dụng thực tế hơn bằng cách

thêm thuộc tính Piority cho lớp Task:

using System;

namespace ToDoApp.Models {

public class Task {

public int Id { get; set; }

public string Content { get; set; } public DateTime CreateDate { get; set; } public DateTime DueDate { get; set; } public int Priority { get; set; } }

}

Khi đã có thuộc tính này, bạn có thể build ứng dụng, tuy nhiên, nếu chưa cập nhật các view thì khi bạn tạo một Task kết quả là Priority sẽ bằng 0.

Mở /Views/Task/Index.cshtml và /Views/Task/Create.cshtml để điều chỉnh, Đầu tiên chúng ta sẽ điều

chỉnh Index.cshtml như sau:

@model IEnumerable<ToDoApp.Models.Task> @{

ViewBag.Title = "Index"; }

<h2>Index</h2> <p>

@Html.ActionLink("Create New", "Create")

</p> <table> <tr> <th> Content </th> <th> CreateDate </th> <th> DueDate </th> <th> Priority </th> <th> Tasks </th> </tr>

@foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Content) </td> <td> @Html.DisplayFor(modelItem => item.CreateDate) </td> <td> @Html.DisplayFor(modelItem => item.DueDate) </td> <td> @Html.DisplayFor(modelItem => item.Priority) </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>

Ở Create.cshtml, chúng ta cần cập nhật để người dùng có thể quy định mức ưu tiên cho tác vụ, đồng thời chúng ta sẽ xóa luôn phần nhập ngày tạo, bởi ngày tạo được xác định theo giờ hệ thống:

@model ToDoApp.Models.Task @{

ViewBag.Title = "Create"; }

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset>

<legend>Task</legend>

<div class="editor-label">

@Html.LabelFor(model => model.Content) </div>

<div class="editor-field">

@Html.EditorFor(model => model.Content)

@Html.ValidationMessageFor(model => model.Content) </div>

<div class="editor-label">

@Html.LabelFor(model => model.Priority) </div>

<div class="editor-field">

@Html.EditorFor(model => model.Priority)

@Html.ValidationMessageFor(model => model.Priority) </div>

<div class="editor-label">

@Html.LabelFor(model => model.DueDate) </div>

<div class="editor-field">

@Html.EditorFor(model => model.DueDate)

@Html.ValidationMessageFor(model => model.DueDate) </div>

<p>

<input type="submit" value="Create" />

</p>

</fieldset>

}

<div>

@Html.ActionLink("Back to List", "Index")

</div>

Ở action Create của TaskController, chúng ta sẽ điều chỉnh lại một chút action Create dành cho POST

request như sau:

[HttpPost]

public ActionResult Create(Task task) {

if (ModelState.IsValid) {

db.Tasks.Add(task); db.SaveChanges();

return RedirectToAction("Index"); }

return View(task); }

Dòng lệnh task.CreateDate = DateTime.Now; được sử dụng để cập nhật ngày tạo của Task trước khi

lưu đối tượng Task lại.

Một phần của tài liệu Tạo ứng dụng ASP mvc (Trang 52 - 55)