.net core mvc初级教程(五)

1、搭建电影院网页目录信息
2、搭建电影院对应电影信息html

以上都是ASP知识,前端部分前端

1、搭建电影院网页目录信息

先,之前写的有点错误,改下,HoemController中git

//添加电影院信息
        public IActionResult add()
        {
            ViewBag.Title = "添加电影院";
            return View(new Cinema());
        }

上面这个改下github

由于咱们之前写的返回视图都是Index,好比
在这里插入图片描述
因此咱们在views文件夹下的home文件夹创建的是Index.cshtml,如今咱们将Index.cshtml之前的信息去掉,
而后咱们视图全部数据对应对应的时Cinema()里面的信息
因此在Index.cshtml中添加@model IEnumerable<CoreDemo.Models.Cinema>
由于他的数据时IEnumerable返回的web

接下来,咱们须要对视图进行布局
而后去bootstrap中找到布局bootstrap

接着坐下table,打开官网,点击左边菜单content->tables
https://getbootstrap.com/docs/4.2/content/tables/
咱们用下这个table
在这里插入图片描述svg

@model IEnumerable<DemoCoreStudy.Models.Cinema>

<div class="container">
    <div class="row">
        <table class="table">
            <thead class="thead-dark">
                <tr>
                    <th scope="col">Id</th>
                    <th scope="col">Name</th>
                    <th scope="col">Location</th>
                    <th scope="col">Capacity</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @Html.DisplayForModel()
            </tbody>
        </table>
    </div>
</div>

2、搭建电影院对应电影信息

tbody这块代码
看看DisplayForModel源码
在这里插入图片描述布局

上面的意思为自定义模板能够在DisplayTemplates文件夹下找到。文件夹名称对大小写敏感
在Home文件夹下建立DisplayTemplates文件夹,添加Cinema.cshtml视图测试

Cinema.cshtml代码.net

@model DemoCoreStudy.Models.Cinema
<tr>
    <td>@Model.Id</td>
    <td>@Model.Name</td>
    <td>@Model.Location</td>
    <td>@Model.Capacity</td>
    <td>
       <a asp-controller="Home" asp-action="Edit" asp-route-cinemaId="@Model.Id">编辑</a>
    </td>
</tr>

这篇代码表示body里面的数据,与@Html.DisplayForModel()相对应,
其中有一行代码可能不理解
asp-controller="Home"是调用home控制器,也就是HomeController
asp-action=“Edit"运用其中Edit方法。emmmm我说的不专业,通俗点说
asp-route-cinemaId="@Model.Id”,asp-route-意思为Edit方法的参数cinemaId等于@Model.Id,也就是Ciname.Id
后面的‘编辑‘意思是这个操做的名字为编辑

而后发现Edit没写进去,打开Homecontroller添加下

public IActionResult Edit(int cinemaId)
        {
            return RedirectToAction("Index");
        }
只作测试用,暂时不加方法

运行

在这里插入图片描述
当当当!!!
能够点击编辑按钮,发现只不过是刷新界面而已
而后发现还有点小问题是Id值没有
去CinemaMemoryService方法中加下Id

而后再运行就有了

下面一片讲对movie的操做 怎样点击电影院能够打开对应电影信息 我是酒窝猪,谢谢坚持到这里的朋友QWQ github代码https://github.com/1045683477/.net-Core-MVC-

相关文章
相关标签/搜索