开发者

asp.net mvc pass a variable to another action method with a form post

I have a page. (开发者_StackOverflow社区action) and a controller called Widget. im passing in client as a string.

I want to be able to pass in the client from one page to the next, as well as the other fields posted.

what am i doing wrong below? client is coming up as null

eg: Widet/Page2/clientABC

        public ActionResult Page2(string client)
        {

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Page2(string client, string sector)
        {
            return RedirectToAction("Page3", new { client = client, sector = sector });
        }

        public ActionResult Page3(string client, string sector)
        {
            return View();
        }


Does this work?

Widet/Page2?client=clientABC&sector=123

Since you have an action with multiple parameters, I think you need to name them in the query string. That's how I've done it. Unless the action has a single parameter, the default routing doesn't handle the way you are trying to call it.

Look in your Global.asax.cs file to see the routing config.

If it looks like this:

    public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

        routes.MapRoute(
          "Root",
          "",
          new { controller = "Home", action = "Index", id = "" }
        );
    }

You could fiddle with it and make it support multiple params like:

{controller}/{action}/{param1}/{param2}

Though I would just use named parameters in the query string.


If I understand this right your problem is that you don't post the client to your "Page2" action. You can ether post it as a post parameter (in a hidden field for example) or in the url (in the action of your form tag). My guess is that you want it in your url.

If you use the form html helper you can use it like this:

<%using(Html.BeginForm("Page2", "Home", new { client = "clientABC" })) { } %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜