Data transfer of MVC controller and view in asp.net core

https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-3.1&tabs=visual-studio (after code verification, all steps are feasible)

In the previous example, the viewdata dictionary object was used to transfer data from the controller to the view.
MVC also provides a way to directly transfer strongly typed objects to views. This allows the compiler to check the type at compile time.

Outgoing data model in controller

You can see the Controllers/MoviesController.cs file

// GET: Movies/Details/5
public async Task<IActionResult> Details(int? id)
{
    if (id == null)
    {
        return NotFound();
    }

    var movie = await _context.Movie
        .FirstOrDefaultAsync(m => m.Id == id);
    if (movie == null)
    {
        return NotFound();
    }

    return View(movie);
}

for example https://localhost:51919/movies/details/1 The controller is movies, the behavior method is details, and the parameter is 1, that is, id=1. You can also use the following string to query the url: https://localhost:51919/movies/details?id=1 The same effect can be achieved.

In FirstOrDefaultAsync is a lambda expression, indicating that the movie entity object satisfying the id is selected. await stands for asynchronous wait.
return View(movie) passes the qualified movie object to the view view.

Using data model objects in views

View Views/Movies/Details.cshtml with the following code

@model MvcMovie.Models.Movie

@{
    ViewData["Title"] = "Details";
}

<h1>Details</h1>

<div>
    <h4>Movie</h4>
    <hr />
    <dl class="row">
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Title)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Title)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.ReleaseDate)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.ReleaseDate)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Genre)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Genre)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Price)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Price)
        </dd>
    </dl>
</div>
<div>
    <a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
    <a asp-action="Index">Back to List</a>
</div>

The first @ model statement indicates that the view requires an object of type MvcMovie.Models.Movie. Then you can use the object directly through @ Model.Id and other methods in the view page, or you can access the object data through @ HTML. Displayfor (model = > model. Price). Here, the lowercase model represents a lambda parameter. In fact, the incoming data is the model.

Controller outgoing list
The above is to return the Model property of a movie object to view through return view, and then access the data through the Model property.
What if the controller returns a List type?
Check the index method in the controller, which is a method to return a list, and of course, it is also an asynchronous return.

// GET: Movies
public async Task<IActionResult> Index()
{
    return View(await _context.Movie.ToListAsync());
}

View incoming list

In Index.cshtml, you can see that @ model IEnumerable & lt; mvcmovie.models.movie & gt; this line of statement indicates that the received type is an interface that implements IEnumerable. List is a class that implements the enumeration interface. So in the view page, you can access the objects in the object list one by one through the loop.

@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ReleaseDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Genre)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
            </td>
        </tr>

Tags: ASP.NET Lambda

Posted on Sat, 25 Apr 2020 20:24:00 -0700 by rrhody