0

In asp.net MVC3, how I can implement an input what it does GET using query parameters of source web page? I prefer don´t use hidden inputs.

Update. For example, my page contains a query parameter id=54, when input causes a GET, I need destination controller/action receive this query parameter.

// could i change it for include query parameters of source page?
using (Html.BeginForm("Action", "Controller", FormMethod.Get))
{   
  <input type="submit" value="Text" onclick="submit"/>
}
fravelgue
  • 2,163
  • 1
  • 20
  • 23
  • Please rephrase the question, I don't understand it. You want to implement an alternative to hidden inputs when posting forms in ASP.NET MVC 3? – Rhapsody Feb 01 '12 at 13:50
  • It seems no possible: http://stackoverflow.com/questions/1116019/submitting-a-get-form-with-query-string-params-and-hidden-params-disappear Could somebody confirm? – fravelgue Feb 01 '12 at 16:21

3 Answers3

1

The solution to your problem is exactly this:

Html.BeginForm("Action", "Controller", new { id = 54 }, FormMethod.Get)
Fabio Milheiro
  • 7,239
  • 13
  • 51
  • 87
  • Note, query parameters in action of forms are ignored. http://www.w3.org/TR/html401/interact/forms.html#form-data-processing – fravelgue Mar 23 '12 at 12:46
  • I'm positive that this works and I didn't find any reason not to in your link. The form in the answer will submit to somewhere like: /Controller/Action?id=54. What's the problem? – Fabio Milheiro Mar 23 '12 at 16:18
  • If you, use HTTP GET and use query parameters in action of forms then browser doesn´t send the query parameters. You can test using fiddler:
    – fravelgue Mar 26 '12 at 07:23
  • Then why does this work? Did you try this? Then how do you submit a form in a page like the old days of site.tld/somepath?p=1&m=45 ? – Fabio Milheiro Mar 26 '12 at 07:26
  • do you use hidden inputs for parameters? – fravelgue Mar 26 '12 at 07:28
  • Hidden parameters is what the user who asked the question didn't want to use and I suggested this way. I have used it in the past and it worked. There's no reason for this not work or there wouldn't even be this overload in the ASP.NET MVC framework! It could be just me but I didn't find any evidence of your statement "query parameters in action of forms are ignored" in the link provided. – Fabio Milheiro Mar 26 '12 at 11:20
0

Use this: ControllerContext.HttpContext.Request.UrlReferrer.ToString(); If it will not help, you can use global filter (register it at global.asax)

protected void Application_Start()
{
    GlobalFilters.Filters.Add(new PreviousUrlSavingFilter())
}

public class PreviousUrlSavingFilter: ActionFilterAttribute
{
     protected override OnActionExecuted(ActionExecutedContext filterContext)
     {
         filterContext.HttpContext.Session["PreviousRouteData"] = filterContext.RouteData;
     }

     // use this property to access previous page route data
     public static RouteData PreviousUrlData
     {
         return (RouteData) HttpContext.Current.Session["PreviousRouteData"];
     }
}

If you want to edit current url using previous url parameters, this link will be helpful to you: https://stackoverflow.com/a/4222584/571203

Community
  • 1
  • 1
Evgeny Levin
  • 6,023
  • 4
  • 43
  • 78
  • Thx for you answer. Good trick ;-) but I think there´ll be another option, I think I prefer hidden inputs to your answer. Some proxies could remove urlreferrer and I only want avoid extra hidden inputs. – fravelgue Feb 01 '12 at 14:52
  • So, can i change url that input GET for including query parameters? – fravelgue Feb 01 '12 at 15:40
  • Dont understand you. Can you explain in details what input you want and what you want to change? – Evgeny Levin Feb 01 '12 at 15:47
0

I would use TempData to store this kind of temporary data, tempdata is saved for one routetrip after its being read it should be disposed unless keep method is used.

DerDee
  • 374
  • 2
  • 10