惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
Project Zero
Project Zero
E
Exploit-DB.com RSS Feed
S
Secure Thoughts
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
WordPress大学
WordPress大学
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hacker News: Front Page
F
Full Disclosure
Latest news
Latest news
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
V
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
P
Privacy International News Feed
C
Check Point Blog
N
News and Events Feed by Topic

博客园 - Bingo Lee

创业这三年¥.NET之尴尬处境 创业这三年@各种奇遇 创业这3年#迈出第一步 传统企业信息化 中国式IT的项目 谁来衡量我们的价值??? 加班?!希望“京东”不是你的最后一站 WinRAR自动备份文件 ASP.NET MVC3 入门指南之数据验证[源码RAR下载] Sql server 实用技巧之主键、系统表与代码生成器[源码+视频] 英孚订课助手 全自动备份vss和sql数据库(含源码下载) Excel插入、更新Orcale Asp.net MVC3.0 入门指南 7.1 展示查找页面 SQL SERVER VS ORCALE(实现已有数据行自增) ASP.NET MVC3.0 入门指南 5 从控制器访问模型数据 Asp.net MVC3.0 入门指南 4 模型 Model Asp.net MVC3.0 入门指南 3.2视图 View Asp.net MVC3.0 入门指南 3.1视图 View
Asp.net MVC3.0 入门指南 6 审视编辑方法和视图
Bingo Lee · 2011-05-13 · via 博客园 - Bingo Lee

2011-05-13 11:23  Bingo Lee  阅读(3152)  评论()    收藏  举报

审视编辑方法和视图

在这一节中,您将审视movie控制器生成的响应方法和视图。然后您将添加

一个自定义搜索页面。

运行程序并通过在URL追加/Moives浏览movie控制器。把鼠标悬停在Edit

链接上,看看它执行的URL.

Edit的链接由视图Views\Movies\Index.cshtml Html.ActionLink方法生成。

@Html.ActionLink("Edit", "Edit", new { id=item.ID }) 

Html对象是一个助手,它是WebViewPage基类暴露的属性。助手的ActionLink方法可以很容易的生成

HTML超级链接,它指向控制器的响应方法。ActionLink的第一个参数超级链接的文本呈现(比如:

<a>Edit Me</a>),第二个参数是要调用的响应方法的名称,最后一个参数生成路由数据的匿名对象

anonymous object,这里指ID=4)。

您可以使用查询字符串(query string)传递参数给响应方法。比如URL

http://localhost:xxxxx/Movies/Edit?ID=4传递ID=4给Movies控制器的Edit方法。

打开Movies控制器。有两个Edit方法,如下所示:

//
// GET: /Movies/Edit/5

public ActionResult Edit(int id) 
{
    Movie movie = db.Movies.Find(id);
    return View(movie);
}

//
// POST: /Movies/Edit/5

[HttpPost]
public ActionResult Edit(Movie movie) 
{
    if (ModelState.IsValid) 
    {
        db.Entry(movie).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(movie);
}

注意第二个Edit方法前面是HttpPost属性。它表明这个重载的Edit方法只能被POST

请求调用。您也可以给第一个Edit方法采用HttpGet属性,但是这不是必须的,应为

方法默认为HttpGet(响应方法隐含的了HttpGet属性将被认为是HttpGet方法)。

HttpGet方法将电影的ID作为参数,并使用实体框架的Find方法查找电影,然后返回被找

到的电影给视图模板。当架构体系创建编辑模板是,它检查Movie类并为每个属性生成

代码去呈现<label><input>元素。下面的代码展示了自动生成的Edit视图模板:

@model MvcMovie.Models.Movie

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Movie</legend>

        @Html.HiddenFor(model => model.ID)

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ReleaseDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ReleaseDate)
            @Html.ValidationMessageFor(model => model.ReleaseDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Genre)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Genre)
            @Html.ValidationMessageFor(model => model.Genre)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<form>中的<input>元素别用来回传页面到电影的编辑地址/Movies/Edit

当点击Edit按钮时,页面的数据被回传到服务器。

处理POST请求

由架构体系生成的属性为HttpGet的Edit方法没有检查传给它的ID的有效性。

如果用户删除URL的ID片段,错误信息如下所示:

用户还可以传递一个不存在的ID,比如:http://localhost:xxxxx/Movies/Edit/1234.您可以给

HttpGet Edit方法做两点修改来限制URL。首先,把ID参数改为默认值为0 (id不是必须传递)。

您也可以在回传电影对象给视图模板之前,检查Find方法是否真正的找到了电影信息。

public ActionResult Edit(int id = 0)
{
    Movie movie = db.Movies.Find(id);
    if (movie == null)
    {
        return HttpNotFound();
    }
    return View(movie);
}

如果没有找到,HttpNotFound方法被调用。

所有的HttpGet方法都遵循类似的模式。它们获取一个电影对象(在Index中返回对象列表),

然后传递模型给视图。Create方法传递一个空电影对象给Create视图。所有的方法(创建、

编辑、删除)都有一个HttpPost的重载方法

在HTTP GET方法中修改数据存在安全风险,在博客

ASP.NET MVC Tip #46 – Don’t use Delete Links because they create Security Holes

中有描述。在HTTP GET方法中修改数据违反了HTTP的最佳实践REST架构模式(其中规定,

GET请求不应改变应用程序状态)。换句话,执行GET操作应该是一个无副作用的安全操作。

下一节:Asp.net MVC3.0 入门指南 7 展示查找页面

微笑

原文网址:http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part6-cs