【发布时间】:2013-05-20 14:23:02
【问题描述】:
我是 ASP.NET 的新手,今天刚开始在 asp.net 上学习 MVC 教程。我到了http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view
到目前为止一切顺利,问题是:
在我看来,我有以下代码 (模型通过@model MyFirstMVC4.Models.Movie 设置为视图)
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Movie</legend>
@Html.HiddenFor(model => model.ID)
//... bla bla html input
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
我的电影控制器
// Shows the view
public ActionResult Edit(int id = 0)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
//
// POST: /Movie/Edit/5
[HttpPost] // Handles the view above
public ActionResult Edit(Movie movie)
{
if (ModelState.IsValid)
{
db.Entry(movie).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
这是一个问题——它到底是如何将 Movie 对象传递给上面的 POST 方法的?!当我观察到客户端有
<form action = "/Movie/Edit/1" ... />
这里我不明白为什么action = url的同一个视图页面?!1 同样在服务器端只有 Html.BeginForm() :( 它是如何实现发布什么动作方法和传递什么路由参数的? 有效,我只是不知道为什么
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-4 razor