如何在表单发布后更改网址

时间:2016-08-02 12:20:01

标签: jquery asp.net-mvc

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的网址;

这可能吗?

2 个答案:

答案 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/Indexyou 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
}

使用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
}

答案 1 :(得分:1)

你可以试试这个:

Response.Redirect("~/");
相关问题