我們都知道,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得到的頁面,是純文本的,沒有鏈接?,F在我想得到神話帶鏈接的神話,我該咋辦呢。。。
請看下面的方法。
打開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 Index
action method, even if you have aHttpPost Index
method.
即不允許表單以post方式提交。
新聞熱點
疑難解答