C# 为什么在使用POST-verb提交表单时再次调用HttpGet方法? 控制器: 视图: //SearhcIndex.cshtml @模型IEnumerable @使用(Html.BeginForm()) {
标题:@Html.TextBox(“搜索字符串”)C# 为什么在使用POST-verb提交表单时再次调用HttpGet方法? 控制器: 视图: //SearhcIndex.cshtml @模型IEnumerable @使用(Html.BeginForm()) {,c#,asp.net-mvc,C#,Asp.net Mvc,标题:@Html.TextBox(“搜索字符串”) } @DisplayNameFor(model=>model.Title) @foreach(模型中的var项目) { @DisplayFor(modeleItem=>item.Title) } 我第一次访问localhost/Movies/SearchIndex,将调用HttpGet SearchIndex,并为相应的视图填充模型。在浏览器上呈现视图后,我输入一个词来过滤列表,然后按提交按钮过滤 我的问题是: 在我看来,点击过滤器将提交
} @DisplayNameFor(model=>model.Title) @foreach(模型中的var项目) { @DisplayFor(modeleItem=>item.Title) } 我第一次访问
localhost/Movies/SearchIndex
,将调用HttpGet SearchIndex
,并为相应的视图填充模型。在浏览器上呈现视图后,我输入一个词来过滤列表,然后按提交按钮过滤
我的问题是:
在我看来,点击过滤器将提交带有POST-verb的表单。但是为什么再次调用HttpGet SearchIndex
呢?我还没有实现HttpPost SearchIndex
(当然)
注意:我是一个新手,请不要在没有给出我可以学习的理由的情况下否决。我正在阅读本教程。之所以这样做,是因为您没有指定它应该只处理get请求,所以默认情况下它将同时处理这两个请求。如果要添加HttpGet
属性,它将具有您所期望的行为
[HttpGet]
public ActionResult SearchIndex(string searchString)
{
var movies = db.Movies.Select(x => x);
if (!string.IsNullOrEmpty(searchString))
movies = movies.Where(x => x.Title.Contains(searchString));
return View(movies);
}
[HttpPost]
public ActionResult SearchIndex(string searchString, string someOtherParam)
{
var movies = db.Movies.Select(x => x);
if (!string.IsNullOrEmpty(searchString))
movies = movies.Where(x => x.Title.Contains(searchString));
//do something different than your get...
return View(movies);
}
请注意,我在这里包括了另一种处理帖子的方法,因为您肯定仍然希望这样做。正如在评论中提到的,如果这仍然失败,请在您的表单中明确指定POST,尽管我从来没有这样做过,我不希望您需要它
还请注意,post方法可能需要不同的名称,也可能需要与get不同的参数(以便.net能够区分它们)
既然您提到自己是一名新手,下面是我更喜欢的MVC教程,让新开发人员在启动MVC时阅读:
如果你还没有这样做,你应该去看看
更新:
根据你的评论,以下是他们在你提供的链接上告诉你的。当请求被路由到控制器时,MVC将选择与请求完全匹配的第一个操作方法。如果未应用[HttpGet]
或[HttpPost]
,则该操作方法将同时接受这两种方法。假设您浏览到/mysite/Movies/SearchIndex。使用上面的示例,这是一个GET请求,因此MVC看到您使用[HttpGet]标记了SearchIndex,这就是它将使用的内容
现在让我们假设您删除了[HttpGet]
,但将[HttpPost]
保留在另一个上(如您提供的文章中的示例所示)。如果你浏览到/mysite/Movies/SearchIndex,MVC会看到标有HTTPPost的SearchIndex()并说,不能使用那个!然后它看到SearchIndex()没有属性(这意味着它接受Post和Get),并使用它
类似地,如果您通过POST请求创建/mysite/Movies/SearchIndex,MVC会发现您有一个标有[HttpPost]
的SearchIndex(),因此它会使用该索引,因为它是完全匹配的
本文未能理解的一点是,方法的默认行为是同时接受get和POST。文章在这一点上实际要说的是,给定了特定的场景,其中一个用[HttpPost]
标记,另一个根本没有标记,默认行为将导致使用未标记的操作方法
如果这让人困惑,我很抱歉,我重写了好几次,但解释起来有点复杂。之所以这样做,是因为您没有指定它应该只处理get请求,所以默认情况下它将同时处理这两个请求。如果要添加HttpGet
属性,它将具有您所期望的行为
[HttpGet]
public ActionResult SearchIndex(string searchString)
{
var movies = db.Movies.Select(x => x);
if (!string.IsNullOrEmpty(searchString))
movies = movies.Where(x => x.Title.Contains(searchString));
return View(movies);
}
[HttpPost]
public ActionResult SearchIndex(string searchString, string someOtherParam)
{
var movies = db.Movies.Select(x => x);
if (!string.IsNullOrEmpty(searchString))
movies = movies.Where(x => x.Title.Contains(searchString));
//do something different than your get...
return View(movies);
}
请注意,我在这里包括了另一种处理帖子的方法,因为您肯定仍然希望这样做。正如在评论中提到的,如果这仍然失败,请在您的表单中明确指定POST,尽管我从来没有这样做过,我不希望您需要它
还请注意,post方法可能需要不同的名称,也可能需要与get不同的参数(以便.net能够区分它们)
既然您提到自己是一名新手,下面是我更喜欢的MVC教程,让新开发人员在启动MVC时阅读:
如果你还没有这样做,你应该去看看
更新:
根据你的评论,以下是他们在你提供的链接上告诉你的。当请求被路由到控制器时,MVC将选择与请求完全匹配的第一个操作方法。如果未应用[HttpGet]
或[HttpPost]
,则该操作方法将同时接受这两种方法。假设您浏览到/mysite/Movies/SearchIndex。使用上面的示例,这是一个GET请求,因此MVC看到您使用[HttpGet]标记了SearchIndex,这就是它将使用的内容
现在让我们假设您删除了[HttpGet]
,但将[HttpPost]
保留在另一个上(如您提供的文章中的示例所示)。如果你浏览到/mysite/Movies/SearchIndex,MVC会看到标有HTTPPost的SearchIndex()并说,不能使用那个!然后它看到SearchIndex()没有属性(这意味着它接受Post和Get),并使用它
类似地,如果您通过POST请求创建/mysite/Movies/SearchIndex,MVC会发现您有一个标有[HttpPost]
的SearchIndex(),因此它会使用该索引,因为它是完全匹配的
本文未能理解的一点是,方法的默认行为是同时接受get和POST。这篇文章在这一点上实际所说的是给定的特定场景,其中一个用[HttpPost]
标记,另一个根本没有标记
[HttpGet]
public ActionResult SearchIndex(string searchString)
{
var movies = db.Movies.Select(x => x);
if (!string.IsNullOrEmpty(searchString))
movies = movies.Where(x => x.Title.Contains(searchString));
return View(movies);
}
[HttpPost]
public ActionResult SearchIndex(string searchString, string someOtherParam)
{
var movies = db.Movies.Select(x => x);
if (!string.IsNullOrEmpty(searchString))
movies = movies.Where(x => x.Title.Contains(searchString));
//do something different than your get...
return View(movies);
}
[HttpPost]
public ActionResult SearchIndex(string searchString)
{
do post stuff here
}
[HttpGet]
public ActionResult SearchIndex(string searchString)
{
var movies = db.Movies.Select(x => x);
if (!string.IsNullOrEmpty(searchString))
movies = movies.Where(x => x.Title.Contains(searchString));
return View(movies);
}