Search code examples
asp.net-mvchttpcontext

how to test form submission in mvc


I'm sending some param via form post from my template and the action in my controller is handling it accordingly.

Now I want to write a test case for that action. How should I make that dummy request so that Request.Form["selectedSuppliersHidden"] will work in controller action?


Solution

  • You shouldn't really need to use Request.Form in MVC

    Give this a read: http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios.aspx

    If you handle posting correctly then you'll have an Action that can accept variables and be tested a lot easier.

    EDIT You can use the FormCollection param in your Action, something like this

    [HttpPost]
    public ActionResult Index(string btnSubmit, FormCollection collection)
    {
        //btnSubmit this is the button that is clicked.
        return View();
    }
    

    The FormCollection will have everything in it from the Request.Form collection. But you should still be able to post the right hand listbox in the normal MVC way