·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> ASP.NEt MVC5--为列表页添加HttpPost方法。

ASP.NEt MVC5--为列表页添加HttpPost方法。

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

asp.net MVC5--为列表页添加HttpPost方法。

我们都知道,get方法,主要用来显示数据,检索之类,对数据一般不会修改。这次我做一个测试为Index方法写一个post方法。

1.get方式

public ActionResult Index(string searchString) { var movies = from m in db.Movies select m;

if (!string.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } return View(movies); }

实现的效果是:

下面我们来看看post方法:

1   [HttpPost]2         public string Index(FormCollection fc, string searchString)3         {4             return "<h3> From [HttpPost]Index: " + searchString + "</h3>";5         }

实现的效果是:

可以看到,现在post得到的页面,是纯文本的,没有链接。现在我想得到神话带链接的神话,我该咋办呢。。。

请看下面的方法。

打开Index视图,修改之前添加的form表单代码成:

1   @using(Html.BeginForm("Index","Movies",FormMethod.Get)){2      <p>3          Title:@Html.TextBox("SearchString")<br />4          <input type="submit" value="筛选" />5     </p>

现在来看一下效果:

Now when you submit a search, the URL contains a search query string. Searching will also go to theHttpGet Indexaction method, even if you have aHttpPost Indexmethod.

即不允许表单以post方式提交。