Controller methods and views
https://docs.asp.net/en/latest/tutorials/first-mvc-app/controller-methods-views.html
We have a good start to the movie app, but the presentation is not ideal. We don’t want to see the time (12:00:00 AM in the image below) and ReleaseDate should be two words.
Open the Models/Movie.cs file and add the highlighted lines shown below:
public class Movie
{
public int ID { get; set; }
public string Title { get; set; } [Display(Name = "Release Date")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
using System.ComponentModel.DataAnnotations;
We’ll cover DataAnnotations in the next tutorial.
The Display attribute specifies what to display for the name of a field (in this case “Release Date” instead of “ReleaseDate”).
The DataType attribute specifies the type of the data, in this case it’s a date, so the time information stored in the field is not displayed.
Browse to the Movies controller and hold the mouse pointer over an Edit link to see the target URL.

The Edit, Details, and Delete links are generated by the MVC Core Anchor Tag Helper in theViews/Movies/Index.cshtml file.
<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>
Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files.
In the code above, the AnchorTagHelper dynamically generates the HTML href attribute value from the controller action method and route id.
You use View Source from your favorite browser or use the F12 tools to examine the generated markup.
The F12 tools are shown below.
Chrome浏览器,在界面上右键,检查:然后会弹出一个浏览界面,左上角的指针图标,单击之后,移动到页面上,会随着移动而展开html

Recall the format for routing set in the Startup.cs file.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
ASP.NET Core translates http://localhost:1234/Movies/Edit/4 into
a request to the Edit action method of the Movies controller with the parameter ID of 4.
(Controller methods are also known as action methods.)
Tag Helpers are one of the most popular new features in ASP.NET Core. See Additional resources for more information.
Open the Movies controller and examine the two Edit action methods:
// GET: Movies/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
} var movie = await _context.Movie.SingleOrDefaultAsync(m => m.ID == id);
if (movie == null)
{
return NotFound();
}
return View(movie);
} // POST: Movies/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,Genre,Price,ReleaseDate,Title")] Movie movie)
{
if (id != movie.ID)
{
return NotFound();
} if (ModelState.IsValid)
{
try
{
_context.Update(movie);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!MovieExists(movie.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction("Index");
}
return View(movie);
}
The [Bind] attribute is one way to protect against over-posting.
You should only include properties in the [Bind] attribute that you want to change.
See Protect your controller from over-posting for more information.
ViewModels provide an alternative approach to prevent over-posting.
Notice the second Edit action method is preceded by the [HttpPost] attribute.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,Genre,Price,ReleaseDate,Title")] Movie movie)
{
if (id != movie.ID)
{
return NotFound();
} if (ModelState.IsValid)
{
try
{
_context.Update(movie);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!MovieExists(movie.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction("Index");
}
return View(movie);
}
The HttpPostAttribute attribute specifies that this Edit method can be invoked only for POST requests.
You could apply the [HttpGet] attribute to the first edit method, but that’s not necessary because [HttpGet] is the default.
The ValidateAntiForgeryTokenAttribute attribute is used to prevent forgery of a request and is paired up with an anti-forgery token generated in the edit view file (Views/Movies/Edit.cshtml).
The edit view file generates the anti-forgery token with the Form Tag Helper.
<form asp-action="Edit">
The Form Tag Helper generates a hidden anti-forgery token that must match the[ValidateAntiForgeryToken] generated anti-forgery token in the Edit method of the Movies controller.
For more information, see
Controller methods and views 控制器方法与视图 2017-3-7 9 分钟阅读时长 作者 By Rick Anderson We have a good start to ... 原文地址: @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.b ... 原文:Controller methods and views 作者:Rick Anderson 翻译:谢炀(Kiler) 校对:孟帅洋(书缘) .张仁建(第二年.夏) .许登洋(Seay) .姚阿勇 ... Create a web app with ASP.NET Core MVC using Visual Studio on Windows 在 windows上用VS创建mvc web app 201 ... View Controller Basics 视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ... MVC stands for model-view-controller. MVC is a pattern for developing applications that are well ar ... View Controller Basics Apps running on iOS–based devices have a limited amount of screen space for d ... About View Controllers View controllers are a vital link between an app’s data and its visual appear ... https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models- ... 使用SecureCRT连接到远程Ubuntu,连接失败报The remote system refused the connection. 进入Ubuntu系统,终端中敲入以下命令: ps -ef|g ... extract函数格式: extract (field from source) extract函数是从日期或者时间数值里面抽取子域,比如年.月.日等.source必须是timestamp.time. ... 工作环境退回到Win7之后,内容查找功能非常不给力,推荐一个文本内容查找工具grepWin. Win7下的文本查找/替换工具: grepWin QT4.8的软件曾经耗费巨大的功夫进行构建,不舍得扔掉!重新安装Qt4.8版本 1.安装qt-creator 安装qt-creator-win-opensource-2.4.0.exe版本,不建议使用 ... 题面 状压好题 1<= n <= 30, 0 <= m <= 30, 1 <= K <= 8 这美妙的范围非状压莫属 理所当然地,0和1代表度的奇偶 dp[i][j ... 文件操作 文件路径 d:\test.txt 编码方式 utf-8 gbk... 操作方式 操作方式:只读,只写,追加,读写,写读..... 以什么编码方式储存的文件,就以什么编码打开进行操作. 只读: ... 15:17 2019/5/10 //第一种 using UnityEngine; using System.Collections; //引入库 using System.Net; using Sys ... <div id="div"> <h2>{{ key }}</h2> </div> new Vue ({ el : "#di ... 高产似母猪..写完上篇看了几集新番就空虚了..零点时分决定爬起来,趁着清明假期能写多写点. 1.前言 我们知道弹出框都是在触发了某种条件后展示,而一个个的新的弹出框的展示,总是覆盖着上一个弹出框.实现 ... 这还是我第一次遇到这个错误,以前都没太注意配置文件中元素的放置顺序.这次在调试一个ASP.NET MVC项目的时候,突然就爆出HTTP Error 500.19错误,提示无法访问请求的页面,因为该页的 ...Controller methods and views的更多相关文章
随机推荐