2012-02-01 62 views
0

在asp.net MVC3中,我如何使用源网页的查询参数来实现输入GET功能?我宁愿不使用隐藏的输入。ASP.NET MVC3输入使用查询参数获取GET

更新。例如,我的页面包含查询参数id = 54,当输入导致GET时,我需要目标控制器/操作接收此查询参数。

// 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"/> 
} 
+0

请重新表述的问题,我不明白。在ASP.NET MVC 3中发布表单时,您希望实现隐藏输入的替代方案吗? – Rhapsody 2012-02-01 13:50:03

+0

这似乎不可能:http://stackoverflow.com/questions/1116019/submitting-a-get-form-with-query-string-params-and-hidden-params-disappear 有人可以确认吗? – fravelgue 2012-02-01 16:21:37

回答

1

解决你的问题正是这一点:

Html.BeginForm("Action", "Controller", new { id = 54 }, FormMethod.Get) 
+0

请注意,表单中的查询参数将被忽略。 http://www.w3.org/TR/html401/interact/forms.html#form-data-processing – fravelgue 2012-03-23 12:46:18

+0

我很积极,这有效,我没有找到任何理由不在你的链接。答案中的表单将提交到某处:/ Controller/Action?id = 54。有什么问题? – 2012-03-23 16:18:46

+0

如果你使用HTTP GET并在表单的动作中使用查询参数,那么浏览器不会发送查询参数。可以测试使用招: \t \t \t <形式行动= “HTTP://本地主机ID = 1” 的方法= “GET”> \t \t \t \t \t \t – fravelgue 2012-03-26 07:23:09

0

使用此:ControllerContext.HttpContext.Request.UrlReferrer.ToString(); 如果它不会帮助,您可以使用全局过滤器(在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"]; 
    } 
} 

如果你想编辑使用以前的URL参数当前的URL,这个链接将对你有所帮助:https://stackoverflow.com/a/4222584/571203

+0

Thx给你回答。好戏;-)但我认为会有另一种选择,我认为我更喜欢隐藏的输入到你的答案。一些代理可能会删除urlreferrer,我只想避免额外的隐藏输入。 – fravelgue 2012-02-01 14:52:36

+0

答复已更新,希望它对您有所帮助。 – 2012-02-01 14:57:22

+0

那么,我可以更改包含查询参数的输入GET的url吗? – fravelgue 2012-02-01 15:40:57

0

我会使用TempData来存储这种临时数据,在读取它之后tempdata被保存为一个routetrip,除非使用keep方法,否则它应该被丢弃。