官术网_书友最值得收藏!

Performing CRUD operations

We have already seen how to create CRUD operations right from scaffolding controllers to their corresponding views for the Blog model, so we will create them for the Post model in this section:

  1. Right-click on the Controllers folder and select Add | New Scaffolded Item.
  2.  Add Scaffold dialog box, select MVC Controller with views, using Entity Framework:
  1. In the Add Controller dialog box, select the appropriate Model class and Data Context class (Post and MasteringEFCoreDbFirstContext in our case) along with the auto-generated controller name, PostsController:
  1. Next click Add as shown in the following screenshot:

Scaffolded items

The Blog URL should be displayed instead of the Blog ID, which was part of the scaffolding. As displaying the ID raises security issues and usability concerns, let's change this mapping to URL.

Let's start our changes from the Index.cshml file, where we have listed Blog.Id instead of Blog.Url

    @foreach (var item in Model) {
<tr>
...
<td>
//@Html.DisplayFor(modelItem => item.Blog.Id)
@Html.DisplayFor(modelItem => item.Blog.Url)
</td>
</tr>
}

The changes are reflected on the screen, where we can see Blog URLs rendered instead of the IDs that were listed before:

Posts list view

We have scaffolded CRUD operations and modified Blog ID usage to URLs in the Post index action. Let's update the same on the other actions as well.

主站蜘蛛池模板: 巫山县| 丰顺县| 且末县| 昌图县| 海安县| 杨浦区| 新巴尔虎右旗| 文昌市| 科尔| 柘城县| 平远县| 苏尼特左旗| 南平市| 鲜城| 万安县| 宁城县| 浪卡子县| 理塘县| 嘉祥县| 屏南县| 双江| 广昌县| 乐至县| 莱芜市| 游戏| 蒙城县| 苍溪县| 新竹市| 桐城市| 宝清县| 吉安市| 双峰县| 澄城县| 莫力| 高台县| 东乌珠穆沁旗| 四会市| 高雄县| 香港| 万盛区| 盐池县|