码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-06-20 22:00:22      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

我们都知道,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 the HttpGet Index action method,  even if you have a HttpPost Index method.

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

 

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

标签:

原文地址:http://www.cnblogs.com/caofangsheng/p/4591049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!