ASP.net MVC控制器采用FormCollection和整数

时间:2011-03-31 20:17:54

标签: asp.net asp.net-mvc visual-studio-2010 parameters

我有一个简单的表单,一个文本框和一个命令按钮。我正在使用method =“post”来获取输入文本框的值到控制器。控制器方法如下所示:

public ActionResult Index(FormCollection form)
        {

这一切都很好,但后来我希望能够使用以下内容:return RedirectToAction(“../ MyFolder / MyView /”+ MyID);但由于我的初始视图(索引)通过传递表单集合,我无法做到上述。我怎样才能做到这一点?任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

我很难理解你的问题,但这是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 });
}

答案 1 :(得分:1)

如果我收到您的问题,您可以将您的方法重写为

public ActionResult Index(int id, FormCollection forms)
    {...}

您可以同时使用formsid