2012-02-01 67 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方法,否則它應該被丟棄。