Home / ASP.NET - MVC / Details method

Learn the Details And View Details method

Now, let's move on to understanding the Details method as well as its View.

1. Re-customize View

From the page Index.cshtml. Then click the link View on the Web interface to go to the deletion page with the link http://localhost:xxxx/Book/Details/13:

In addition to customizing English to Vietnamese for the deletion form, we also customize some of the following tags:

To display the image we will change @Html.DisplayFor(model => model.Images)to

 

In the content description section, html tags are also displayed. We will change @Html.DisplayFor(model => model.Description)to @Html.Raw(Model.Description)

@Html.Raw(Model.Description)

Details.cshtml page source code after customization:

@model BookStoreManager.Models.Book

@{
    ViewBag.Title = "Details";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Book details


@Html.DisplayNameFor(model => model.Title)
@Html.DisplayFor(model => model.Title)
@Html.DisplayNameFor(model => model.Price)
@Html.DisplayFor(model => model.Price)
@Html.DisplayNameFor(model => model.Images)
@Html.DisplayNameFor(model => model.Description)
@Html.Raw(Model.Description)
@Html.DisplayNameFor(model => model.Published)
@Html.DisplayFor(model => model.Published)
@Html.DisplayNameFor(model => model.ViewCount)
@Html.DisplayFor(model => model.ViewCount)
@Html.DisplayNameFor(model => model.Author.AuthorName)
@Html.DisplayFor(model => model.Author.AuthorName)
@Html.DisplayNameFor(model => model.Category.CategoryName)
@Html.DisplayFor(model => model.Category.CategoryName)

@Html.ActionLink("Edit", "Edit", new { id = Model.BookID }) | @Html.ActionLink("Return to list", "Index")

From the page Index.cshtml. Then click the link View on the Web interface to go to the edit page the link http://localhost:xxxx/Book/Edit/13 will call ActionResult Details(int? id)in the controller BookController.cs Gives the result after customizing edit Details.cshtml.


2. Details

method

Open the file BookController.cs. Next we will do the following in Controller , looking for the two methods Details(int? id):

public ActionResult Details(int? id)
{
  if (id == null)
   {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
   }
   Book book = db.Books.Find(id);
   if (book == null)
    {
        return HttpNotFound();
    }
   return View(book);
}

The Details(int? id) method is used to display any book information by ID index. In this method, you can check whether the input value is null or not. If not, use the Find method to find whether the Book object is in the database by ID or not. If so, display it, otherwise return the HttpNotFound() method, meaning not found.

A little aside, type int? means the variable can be either int or null.

int i1 = 1; //OK

int i2 = null; //not OK

int? i3 = 1; //OK

int? i4 = null; //OK, int? allow null variables

If a Book is found by its id index, a Book model instance is pushed to the View with the line return View(book).

Did you find this article helpful?

Support the author so they can write more quality articles!

Tư vấn lộ trình CNTT 🤖