你的URL与参数链接到一个GET操作,对不对?如果是这样,请将参数名称作为varaible添加到该Action的声明中。因此,例如,说我的网址通过电子邮件发送的是:
http://mywebsite.com/register?id=511&sl=department
然后我相应的行动是:
public ActionResult Register(int id, string sl)
{
MyModel myModel = new MyModel();
myModel.id = id;
myModel.sl = sl;
return View(myModel);
}
为了保持过程中多步骤的过程,你可以使用这些Html.HiddenFor()在视图中添加隐藏字段或保存到其他位置(即数据库)。
如果你不希望将其添加到您的模型,那么你可以这样做:
public ActionResult Register(int id, string sl)
{
ViewData["id"] = id;
ViewData["sl"] = sl;
return View();
}
现在在视图中有一个隐藏字段每个。然后在POST-to控制器操作中:
[HttpPost]
public ActionResult Register(MyModel myModel, int id, string sl)
{
// the hidden fields are now in id and sl
// ASSUMPTION: the names of "id" and "sl" don't exist in MyModel -- if they do, collision
...
return View();
}
问题是查询字符串是在URL中编码的。如果您不从URL导航(例如,通过重定向或设置JS中的URL),浏览器将“保持放置”该URL。因此,要“保留”查询字符串参数,直到您发布在同一页面(URL)上,您不必做任何特别的事情!关于如何访问这些查询字符串参数已经有很多很好的答案,并且对于初始GET和以下POST操作同样适用。 –