Jquery 在表单发布后如何更改url var url='1〕http://localhost:55026/Home/getData'; var hiddendata=$(“”).attr(“名称”、“代码段”).attr(“值”、“内容”).attr(“类型”、“隐藏”) 变量形式=$(''+''); $(表格).append(隐藏数据); $('body')。追加(表格); 表单提交(); [HttpPost,ValidateInput(false)] 公共ActionResult getData(字符串片段) { }
从控制器获取值后,我需要更改url,如Jquery 在表单发布后如何更改url var url='1〕http://localhost:55026/Home/getData'; var hiddendata=$(“”).attr(“名称”、“代码段”).attr(“值”、“内容”).attr(“类型”、“隐藏”) 变量形式=$(''+''); $(表格).append(隐藏数据); $('body')。追加(表格); 表单提交(); [HttpPost,ValidateInput(false)] 公共ActionResult getData(字符串片段) { },jquery,asp.net-mvc,Jquery,Asp.net Mvc,从控制器获取值后,我需要更改url,如http://localhost:55026而不是http://localhost:55026/Home/getData 这可能吗?您可以在HttpPost操作方法中调用重定向到操作方法 var url = 'http://localhost:55026/Home/getData'; var hiddendata = $("<input>").attr("name","snippet").attr('value',"content").attr(
http://localhost:55026
而不是http://localhost:55026/Home/getData
这可能吗?您可以在HttpPost操作方法中调用
重定向到操作
方法
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)
{
}
这将向浏览器发送302响应,位置标题值为/Home/Index
,浏览器将向该位置发出新的GET请求
根据路由配置,假设Home/Index
是您的默认操作
如果要在Home/Index
中显示数据
带有查询字符串
[HttpPost, ValidateInput(false)]
public ActionResult getData(string snippet)
{
//to do : Do something with posted data
return RedirectToAction("Index","Home");
}
现在在您的索引操作中,添加一个名为id的参数,您可以在那里读取值
return RedirectToAction("Index","Home",new {id=snippet});
使用TempData
public ActionResult Index(string id="")
{
//check the value in id param. you may pass to your view as needed
}
现在在索引操作中
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
}
执行重定向。感谢您的响应感谢您的响应。在这里,我无法将数据传递给查看公共ActionResult getData(字符串片段){return view(“Index”);}。我已按照您的建议进行了尝试。但我无法将值传递给view。它直接重定向到“您最初的问题是如何执行重定向,如果要显示数据,需要执行
returnview()
并根据需要将数据传递给视图。url将是/Home/getData
。如果您希望URL只是<代码>您的站点名,可以考虑使用<代码>索引>代码> > <代码>首页>代码>控制器来替换<代码> GETDATABOS/COD> >方法。使用默认路由配置,urlyourSiteName
(无任何控制器/操作)将由Home/Index
处理。感谢您的响应感谢您的响应。当然。这有助于重定向链接。但在这里我无法传递表单数据以查看。请参阅我的代码。公共操作结果getData(字符串片段){return View(“Index”);}。我已经按照您的建议进行了尝试。但是我无法将值传递给View。它直接重定向到“localhost:55026”;如果它是一个简单的字符串或int值,您可以将其存储在Tempdata中,并在视图中访问它?感谢您的响应
Response.Redirect("~/");