采用FormCollection和整数的ASP.net MVC控制器
我有一个简单的表单,一个文本框和一个命令按钮。我使用method=“post”将输入文本框的值获取到控制器。控制器方法如下所示:采用FormCollection和整数的ASP.net MVC控制器,asp.net,asp.net-mvc,visual-studio-2010,parameters,Asp.net,Asp.net Mvc,Visual Studio 2010,Parameters,我有一个简单的表单,一个文本框和一个命令按钮。我使用method=“post”将输入文本框的值获取到控制器。控制器方法如下所示: public ActionResult Index(FormCollection form) { 这一切都很好,但稍后我希望能够使用以下命令:return RedirectToAction(“../MyFolder/MyView/”+MyID);但由于我的初始视图(索引)是通过传递表单集合来工作的,所以我无法执行上述操作。我怎样才能做到这一点?任何帮
public ActionResult Index(FormCollection form)
{
这一切都很好,但稍后我希望能够使用以下命令:return RedirectToAction(“../MyFolder/MyView/”+MyID);但由于我的初始视图(索引)是通过传递表单集合来工作的,所以我无法执行上述操作。我怎样才能做到这一点?任何帮助都将不胜感激 我很难理解您的问题,但这里有一个ASP.NET MVC中常用的模式,您可能会有所帮助:
// Used to render some form allowing to edit a model
public ActionResult Index(int id)
{
var model = _someRepository.Get(id);
return View(model);
}
// used to handle the submission of the form
[HttpPost]
public ActionResult Index(SomeViewModel model)
{
if (!ModelState.IsValid)
{
// the model was invalid => redisplay the form insulting the user
return View(model);
}
// TODO: the user entered valid information => process the model
// and redirect
return RedirectToAction("Index", new { id = model.Id });
}
我很难理解您的问题,但这里有一个ASP.NET MVC中常用的模式,您可能会有所帮助:
// Used to render some form allowing to edit a model
public ActionResult Index(int id)
{
var model = _someRepository.Get(id);
return View(model);
}
// used to handle the submission of the form
[HttpPost]
public ActionResult Index(SomeViewModel model)
{
if (!ModelState.IsValid)
{
// the model was invalid => redisplay the form insulting the user
return View(model);
}
// TODO: the user entered valid information => process the model
// and redirect
return RedirectToAction("Index", new { id = model.Id });
}
如果我有你的问题,你可以重写你的方法
public ActionResult Index(int id, FormCollection forms)
{...}
然后您可以同时使用
表单
和id
如果我收到了您的问题,您可以将您的方法重写为
public ActionResult Index(int id, FormCollection forms)
{...}
然后,您可以同时使用
表单
和id
我尝试了以下代码片段,我有这些代码尝试重定向到一个方法。返回重定向操作(“../MyFolder/Index/”+ID);当我在这里放置断点时,我在ID内有一个值。。当它运行时,它进入公共操作结果索引(intid){但它说id为空。@Cal,你知道为什么吗?@Cal,你注意到我在回答中使用RedirectToAction
方法了吗?像这样return RedirectToAction(“Index”,new{id=id})
而不是return RedirectToAction(../MyFolder/Index“+id})
。您还有一个重载,如果与当前控制器不同,它将使用控制器名称。返回重定向操作(“../MyFolder/Index/”+ID);当我在这里放置断点时,我在ID内有一个值。。当它运行时,它进入公共操作结果索引(intid){但它说id为空。@Cal,你知道为什么吗?@Cal,你注意到我在回答中使用RedirectToAction
方法了吗?像这样return RedirectToAction(“Index”,new{id=id})
而不是return RedirectToAction(../MyFolder/Index“+id})
。您还有一个重载,如果与当前控制器不同,它将使用控制器名称。