0

I am trying to use a simple form with only a text field to get some information that will be used in an action method to redirect to a different action method. Here's the context:

I have a route mapped in my global.asax.cs file which prints "moo" the given amount of times. For example, if you typed "www.cows.com/Moo8", "Moo" would be printed 8 times. The number is arbitrary and will print however many "Moo"s as the number in the URL. I also have a form on the homepage set up as follows:

@using (Html.BeginForm("Moo", "Web"))
{
    <text>How many times do you want to moo?</text>
    <input type="text" name="mooNumber" />
    <input type="submit" value="Moo!" />
}

The number submitted in the form should be sent to the action method "Moo" in the "Web" controller (WebController.cs):

 [HttpPost]
        public ActionResult Moo(int mooNumber)
        {
            Console.WriteLine(mooNumber);
            return RedirectToAction("ExtendedMoo", new { mooMultiplier = mooNumber });           
        }

Finally, the "Moo" action method should send me back to the original "www.cows.com/Moo8" page; as you can see above I simply used an already existing action method "ExtendedMoo":

 public ViewResult ExtendedMoo(int mooMultiplier)
        {
            ViewBag.MooMultiplier = RouteData.Values["mooMultiplier"];

            return View();
        }

How can I access the value submitted in my form and use it in the last call to "ExtendedMoo"?

Chris V.
  • 1,123
  • 5
  • 22
  • 50

4 Answers4

0

If I understood right

You can you use form Collection to get the value from textbox.

Make Sure the input tag has both id and name properties mentioned otherwise it wont be available in form collection.

    [HttpPost]
    public ActionResult Moo(int mooNumber, **formcollection fc**)
    {
        **string textBoxVal= fc.getvalue("mooNumber").AttemptedValue;**
        Console.WriteLine(mooNumber);
        return RedirectToAction("ExtendedMoo", new { mooMultiplier = mooNumber });           
    }
KNC
  • 94
  • 5
0

Refer to this post or this, you might get some idea how routing works. Something is wrong with "www.cows.com/Moo8", try to find it out. Hint "{controller}/{action}/{parameter_or_id}"

Community
  • 1
  • 1
bjan
  • 2,000
  • 7
  • 32
  • 64
  • You're misunderstanding the question. I understand routing, and the route is fine, I've tested it. It works. The question is not about routing, it's about how to get the values from the form. The route is actually this: routes.MapRoute( "ExtendedMooCount", // Route name "Moo{mooMultiplier}", // URL with parameters new { controller = "Web", action = "ExtendedMoo" }, // Parameter defaults new { mooMultiplier = @"\d+" } // RegEx constraint: digit(s) only ); – Chris V. Feb 03 '12 at 05:34
  • Ok, my bad, then try [this post](http://stackoverflow.com/questions/1257482/redirecttoaction-with-parameter). it looks similar – bjan Feb 03 '12 at 05:40
  • the thing is, I already have that, as you can see in my original post as part of my "Moo" action method (middle code-block). However it seems my code never reaches the method though when the form is submitted, leading me to believe either that isn't right or I'm missing something. – Chris V. Feb 03 '12 at 05:45
  • Follow [this post](http://stackoverflow.com/questions/6825985/how-to-send-text-box-value-as-parameter-on-form-submit-in-mvc3-asp-net). I have tested it and it is working. Whatever i type in the textbox is returned as the parameter to my action – bjan Feb 03 '12 at 06:51
  • Now I'm *completely* at a loss -- that's exactly what I have >_< Still not working. Lameeee... :( – Chris V. Feb 03 '12 at 07:00
0

Instead of RedirectToAction, use Redirect and create the Url. This should do the trick:

return Redirect(Url.RouteUrl(new { controller = "Web", action = "ExtendedMoo", mooMultiplier = mooNumber }));

I hope i helps.

Iridio
  • 9,213
  • 4
  • 49
  • 71
  • Looks helpful, but it seems to me that the "Moo" method that sends the redirect isnt even being reached. As you can see, I put a Console.WriteLine() in the method so I could tell whether it was being reached, and it doesn't print. I can only infer from this that the problem is in my form declaration - the first block of code in my original post. – Chris V. Feb 03 '12 at 05:59
  • Looking at your code to me should work like this: You submit to Moo method. Moo method to the console.WriteLine than call "return Redirect(Url.RouteUrl....). It should work as expected. If your submit don't reach the Moo method maybe you have a problem in the global.asax with the routing rules? – Iridio Feb 03 '12 at 06:11
  • But shouldnt the form submit button at least get to the Moo method, since that's the one I'm telling it to send the Post request to? That shouldn't have anything to do with the routing, at least not yet at that point. – Chris V. Feb 03 '12 at 06:18
  • The form should call Moo method since is what you are telling it to do with @using (Html.BeginForm("Moo", "Web")). If this call do not reach the method I guess is a problem of route rules. Another try would be tochange "public ActionResult Moo(int mooNumber)" to "public ActionResult Moo(string mooNumber)" – Iridio Feb 03 '12 at 06:33
  • Tried changing the int to a string, no luck with that either. :/ Could you explain to me why the routing rules would affect the form from posting? As I understand it the routing rules shouldn't even come into effect until at least when I redirect after the post. – Chris V. Feb 03 '12 at 06:47
  • The beginForm generate the url like html.Action. It will look in global.asax for how to create the url. Look at the rendered html and see what's the output. If your rule is right you should have something like
    or whatever you coded in global.asax
    – Iridio Feb 03 '12 at 06:56
0

Oh wow. Turns out that form was on my Homepage, so instead of using "Moo" as the action method, I needed to override the "Homepage" action method with a [HttpPost] annotation over THAT one. Didn't realize that forms submitted to the page they were rendered from - that was a really useful piece of information in solving this problem!

Thanks all for your attempts at helping out!

Chris V.
  • 1,123
  • 5
  • 22
  • 50