日期:2014-05-17  浏览次数:20433 次

BeginForm简单却郁闷的要求

@Html.BeginForm("index", "Home", FormMethod.Get)
{
<input type="text" id="text1" />
 <input type="submit" value="submit"/>
}


动作
 public ActionResult(string words)
{
  string str=words;
  return view();
}

要求就是,从input 标签中获取输入的值,并且通过get方式传到控制器动作上,最好是直接就传给 words 这个参数,就是SO easy 但不知道怎么弄.

------最佳解决方案--------------------
你把文本框的name设置成和参数一样的名字,比如你这里:<input type="text" id="text1" name="words"/>

修改成上面的试试
------其他解决方案--------------------
class HomeController {
[Get]
public ActionResult Index()
{
    return View();
}
[Post]
public string Index(FormCollection collection)
{
  string str = collection["text1"];
  return View(str);
}
}