- Mastering Entity Framework Core 2.0
- Prabhakaran Anbazhagan
- 252字
- 2021-07-02 21:16:41
Creating controller action
The changes we made in the previous section need to be updated in the SelectList collection, which will be used by MVC to render the drop-down list. By default, MVC scaffolding provides a SelectList (we have commented on that line) that has Id in the Value field and needs to be modified to Url, otherwise it will display only numeric values on the screen (a serious security issue and not so user-friendly):
public IActionResult Create()
{
//ViewData["BlogId"] = new SelectList(_context.Blog, "Id", "Id");
ViewData["BlogId"] = new SelectList(_context.Blog, "Id", "Url");
return View();
}
The following screenshot shows the Url mapped to the BlogId control, but there is something additional that needs to be fixed. The BlogId should be either just Blog or Blog URL.
I will leave this part as an exercise, kindly make changes to all the labels associated with the BlogId column:

Posts create view
The same change needs to be applied to the Post action of Create as well:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult>
Create([Bind("Id,BlogId,Content,PublishedDateTime,Title")]
Post post)
{
...
ViewData["BlogId"] = new SelectList(_context.Blog, "Id", "Url",
post.BlogId);
return View(post);
}
The changes are reflected in the newly included/added items displayed on the screen (the Index action was already modified to list the Blog URLs):

List view with new post
We have updated the references to the Blog ID with Blog URLs in the Post Create (HTTP, GET, and POST) action. Let's update the same on the other actions as well.
- Mastering OpenLayers 3
- 前端跨界開發(fā)指南:JavaScript工具庫(kù)原理解析與實(shí)戰(zhàn)
- 計(jì)算機(jī)圖形學(xué)編程(使用OpenGL和C++)(第2版)
- Getting Started with ResearchKit
- 新手學(xué)Visual C# 2008程序設(shè)計(jì)
- OpenNI Cookbook
- INSTANT Weka How-to
- 數(shù)據(jù)結(jié)構(gòu)與算法JavaScript描述
- Java應(yīng)用開發(fā)技術(shù)實(shí)例教程
- Scala編程實(shí)戰(zhàn)(原書第2版)
- Lighttpd源碼分析
- C# 7.0本質(zhì)論
- RESTful Web API Design with Node.js
- KnockoutJS Blueprints
- SQL Server 2014數(shù)據(jù)庫(kù)設(shè)計(jì)與開發(fā)教程(微課版)