开发者

ASP.net MVC Controller Taking a FormCollection AND an Integer

开发者 https://www.devze.com 2023-02-21 10:18 出处:网络
I have a simple form, a text box and a command button. I am using a method=\"post\" to get the value which is entered into the textbox to the controller. The controller method looks like this:

I have a simple form, a text box and a command button. I am using a method="post" to get the value which is entered into the textbox to the controller. The controller method looks like this:

public ActionResult Ind开发者_C百科ex(FormCollection form)
        {

This is all fine, but later down the line I want to be able to use the following: return RedirectToAction("../MyFolder/MyView/" + MyID); but because my initial view (Index) worked by passing a form collection, I cannot do the above. How can I make this possible? any help would be greatly appreciated!


I can hardly make any sense out of your question but here's a commonly used pattern in ASP.NET MVC which you might be helpful:

// 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 });
}


If i got your question, you can rewrite your method as

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

Than you can use both forms and id

0

精彩评论

暂无评论...
验证码 换一张
取 消