MVC .NET"编辑"按钮选择正确的方法 [英] MVC .NET "Edit" button choose right method

查看:127
本文介绍了MVC .NET"编辑"按钮选择正确的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这个教程>> <一个href=\"http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view\" rel=\"nofollow\">http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view

和我有一个问题让我不解!

在我的控制器类MoviesController我有一个方法,编辑和过载,这些方法之一,当用户开始编辑一条记录被使用和另一个用于发布的更改。

我的问题是如何页知道它必须调用,我知道哪些方法是使用参数,但希望code我没有发现任何可能跟踪要求。

下面那张code:

Edit.cshtml

  @model SmartJob.Models.Movie@ {
    ViewBag.Title =编辑;
}&LT; H2&gt;编辑&LT; / H&GT;@using(Html.BeginForm())
{
    @ Html.AntiForgeryToken()    &LT; D​​IV CLASS =形横&GT;
        &LT; H4&GT;电影&LT; / H4&GT;
        &LT;小时/&GT;
        @ Html.ValidationSummary(真)
        @ Html.HiddenFor(型号=&GT; model.ID)        &LT; D​​IV CLASS =表单组&GT;
            @ Html.LabelFor(型号=&GT; model.Title,新{@class =控制标签COL-MD-2})
            &LT; D​​IV CLASS =COL-MD-10&GT;
                @ Html.EditorFor(型号=&GT; model.Title)
                @ Html.ValidationMessageFor(型号=&GT; model.Title)
            &LT; / DIV&GT;
        &LT; / DIV&GT;        &LT; D​​IV CLASS =表单组&GT;
            @ Html.LabelFor(型号=&GT; model.ReleaseDate,新{@class =控制标签COL-MD-2})
            &LT; D​​IV CLASS =COL-MD-10&GT;
                @ Html.EditorFor(型号=&GT; model.ReleaseDate)
                @ Html.ValidationMessageFor(型号=&GT; model.ReleaseDate)
            &LT; / DIV&GT;
        &LT; / DIV&GT;        &LT; D​​IV CLASS =表单组&GT;
            @ Html.LabelFor(型号=&GT; model.Genre,新{@class =控制标签COL-MD-2})
            &LT; D​​IV CLASS =COL-MD-10&GT;
                @ Html.EditorFor(型号=&GT; model.Genre)
                @ Html.ValidationMessageFor(型号=&GT; model.Genre)
            &LT; / DIV&GT;
        &LT; / DIV&GT;        &LT; D​​IV CLASS =表单组&GT;
            @ Html.LabelFor(型号=&GT; model.Price,新{@class =控制标签COL-MD-2})
            &LT; D​​IV CLASS =COL-MD-10&GT;
                @ Html.EditorFor(型号=&GT; model.Price)
                @ Html.ValidationMessageFor(型号=&GT; model.Price)
            &LT; / DIV&GT;
        &LT; / DIV&GT;        &LT; D​​IV CLASS =表单组&GT;
            &LT; D​​IV CLASS =COL-MD-偏移2 COL-MD-10&GT;
                &LT;输入类型=提交值=保存级=BTN BTN-默认的/&GT;
            &LT; / DIV&GT;
        &LT; / DIV&GT;
    &LT; / DIV&GT;

MoviesContoller类(仅限编辑方法)

  // GET:/电影/编辑/ 5
        公众的ActionResult编辑(INT?ID)
        {
            如果(ID == NULL)
            {
                返回新的HTTPStatus codeResult(的HTTPStatus code.BadRequest);
            }
            电影电影= db.Movies.Find(ID);
            如果(电影== NULL)
            {
                返回HttpNotFound();
            }
            返回查看(电影);
        }        // POST:/电影/编辑/ 5
        //从overposting攻击保护,请启用要绑定到特定的属性,
        //更多详情请参阅http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        公众的ActionResult编辑([绑定(包括=ID,标题,RELEASEDATE,流派,价格)电影的电影)
        {
            如果(ModelState.IsValid)
            {
                db.Entry(电影).STATE = EntityState.Modified;
                db.SaveChanges();
                返回RedirectToAction(「指数」);
            }
            返回查看(电影);
        }


解决方案

默认情况下,没有一个属性的任何请求[HTTPGET]或[HttpPost]将是一个GET方法。您必须指定该方法是一个[HttpPost]如果你正在张贴对方法(又名提交某种形式的)

请注意,没有 [HTTPGET] [HttpPost] 在你第一次编辑方法指定。因此,它会默认为 [HTTPGET]

  // GET:/电影/编辑/ 5
        公众的ActionResult编辑(INT?ID)
        {
            如果(ID == NULL)
            {
                返回新的HTTPStatus codeResult(的HTTPStatus code.BadRequest);
            }
            电影电影= db.Movies.Find(ID);
            如果(电影== NULL)
            {
                返回HttpNotFound();
            }
            返回查看(电影);
        }

现在发现这个方法怎么了的标签[HttpPost] 。您还可以指定其他动词如 [HttpPut] [HttpDelete] 等等,如果你需要,但这些是最​​流行的是使用的。另请注意,您有 [装订()] 方法。这是用来prevent发送太多数据时,你可能只需要更新对象的几个字段。

  // POST:/电影/编辑/ 5
        //从overposting攻击保护,请启用要绑定到特定的属性,
        //更多详情请参阅http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        公众的ActionResult编辑([绑定(包括=ID,标题,RELEASEDATE,流派,价格)电影的电影)
        {
            如果(ModelState.IsValid)
            {
                db.Entry(电影).STATE = EntityState.Modified;
                db.SaveChanges();
                返回RedirectToAction(「指数」);
            }
            返回查看(电影);
        }

I'm following this tutorial >> http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view

And i got a question that makes me puzzled!

In my controller class "MoviesController" I got a method Edit and an overload, one of these methods is used when the user start to editing a record and another used to post the changes.

My question is how the page knows which method it must calls, I know that's using the parameters, but looking to code I didn't find anything that could track the request.

Here goes the code:

Edit.cshtml

@model SmartJob.Models.Movie

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Movie</h4>
        <hr />
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.ID)

        <div class="form-group">
            @Html.LabelFor(model => model.Title, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title)
                @Html.ValidationMessageFor(model => model.Title)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ReleaseDate, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ReleaseDate)
                @Html.ValidationMessageFor(model => model.ReleaseDate)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Genre, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Genre)
                @Html.ValidationMessageFor(model => model.Genre)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Price)
                @Html.ValidationMessageFor(model => model.Price)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>

MoviesContoller class (only Edit methods)

 // GET: /Movies/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Movie movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }
            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 ActionResult Edit([Bind(Include="ID,Title,ReleaseDate,Genre,Price")] Movie movie)
        {
            if (ModelState.IsValid)
            {
                db.Entry(movie).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(movie);
        }

解决方案

By default, any requests without an attribute of [HttpGet] or [HttpPost] will be a GET method. You must specify that the method is a [HttpPost] if you are posting against the method(Aka submitting some type of form)

Notice that there is no [HttpGet] or [HttpPost] specified in your first Edit method. Thus it will default to [HttpGet]

// GET: /Movies/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Movie movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }
            return View(movie);
        }

Now notice how this method has a tag of [HttpPost]. You can also specify other verbs such as [HttpPut], [HttpDelete] and many more if you need to, but these are the most popular that are used. Also notice that you have the [Bind()] method. This is used to prevent sending "Too" much data when you might only want to update a few fields of the object.

        // 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 ActionResult Edit([Bind(Include="ID,Title,ReleaseDate,Genre,Price")] Movie movie)
        {
            if (ModelState.IsValid)
            {
                db.Entry(movie).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(movie);
        }

这篇关于MVC .NET&QUOT;编辑&QUOT;按钮选择正确的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆