2016-08-02 19 views
1
var url = 'http://localhost:55026/Home/getData'; 
var hiddendata = $("<input>").attr("name","snippet").attr('value',"content").attr('type', 'hidden') 
var form = $('<form action="' + url + '" method="post" target="_blank" >' + '</form>'); 
$(form).append(hiddendata); 
$('body').append(form); 
form.submit(); 


[HttpPost, ValidateInput(false)] 
public ActionResult getData(string snippet) 
{ 
} 

從控制器獲得價值後,我需要更改像http://localhost:55026而不是http://localhost:55026/Home/getData這樣的網址;我如何更改表格後的網址

這可能嗎?

+1

執行重定向。 –

+0

感謝您的回覆 –

回答

1

您可以在您的HttpPost操作方法中調用RedirectToAction方法。

[HttpPost, ValidateInput(false)] 
public ActionResult getData(string snippet) 
{ 
    //to do : Do something with posted data 
    return RedirectToAction("Index","Home"); 
} 

這將發送一個302響應於與所述位置標頭值作爲/Home/Index,瀏覽器將進行新的GET請求到該位置的瀏覽器。

假設Home/Index是您按照路由配置的默認操作。

如果你想顯示Home/Index數據,you may pass it using querystring /TempData

隨着查詢字符串

return RedirectToAction("Index","Home",new {id=snippet}); 

現在索引中的作用,添加參數名爲id,你可以讀出值有

public ActionResult Index(string id="") 
{ 
    //check the value in id param. you may pass to your view as needed 
} 

With TempData

TempData["Snippet"] = snippet; 
return RedirectToAction("Index","Home"); 

現在,在你的索引行動

public ActionResult Index() 
{ 
    var id=TempData["Snippet"] as string; 
    //check the value in id variable. you may pass to your view as needed 
} 
+0

感謝您的回覆。在這裏,我不能傳遞數據來查看公衆的ActionResult getData(字符串片段) {返回視圖(「索引」);}我試着按照你的建議。但我無法傳遞值來查看。它直接重定向到'http:// localhost:55026' –

+0

你最初的問題是如何做重定向,如果你想顯示數據,你需要'返回View() '並根據需要將數據傳遞給視圖。該網址將成爲'/ Home/getData'。如果你想讓url只是'yourSiteName',你可以考慮用'Home'控制器的'Index'操作替換'getData'方法。使用默認路由配置,「yourSiteName」網址(沒有任何控制器/操作)將由'Home/Index'處理。 – Shyju

+1

感謝您的回覆 –

1

你可以試試這個:

Response.Redirect("~/"); 
+0

感謝您的回覆。 –

+0

當然,這有助於重定向link.but在這裏,我不能通過表單數據來查看。請參考我的code.public ActionResult getData(字符串片段){返回視圖(「索引」);}。我試過根據你的建議,但我不能通過價值view.it直接重定向到'localhost:55026'; –

+0

如果它是一個簡單的字符串或int值,您可以存儲在Tempdata中並在View中訪問相同的值? –