我在JavaScript中有以下方法:
var string1 = "testing";
var date = "10/10/2012";
var win = window.open(BuildUrl(("report", "report"), "myreports",
"toolbar=0,statusbar=0,scrollbars=1,resizable=1,location=0");
function BuildUrl(controllerName, actionName) {
var TimeStamp = Number(new Date());
var win = window.location;
return win.protocol + "//" + win.host + "/" + controllerName + "/" + actionName + '?_=' + TimeStamp ;
}
C#控制器中的方法如下所示:
public ActionResult report()
{
return View();
}
现在,当访问URL到C#方法时,我需要传递名称和日期等参数。我怎么能这样做?
答案 0 :(得分:2)
以下列格式将参数附加到URL:
<url>?param1=value1¶m2=value...
如果需要传递值数组,则使用相同的参数名称
<url>?arr=value1&arr=value2...
所以你的网址看起来像是
domain.com/Controller/Report?name=xyz&date=20
更新
要在Action中接收它们,请声明传递给Action的参数。
public ActionResult Report(string name, DateTime date)
{
...
}
阅读ASP.NET MVC中的Model Binding
答案 1 :(得分:1)
我找到了一种更好的方法将参数传递给弹出窗口,甚至从中检索参数:
在主页:
var popupwindow;
var sharedObject = {};
function openPopupWindow()
{
// Define the datas you want to pass
sharedObject.var1 =
sharedObject.var2 =
...
// Open the popup window
window.open(URL_OF_POPUP_WINDOW, NAME_OF_POPUP_WINDOW, POPUP_WINDOW_STYLE_PROPERTIES);
if (window.focus) { popupwindow.focus(); }
}
function closePopupWindow()
{
popupwindow.close();
// Retrieve the datas from the popup window
= sharedObject.var1;
= sharedObject.var2;
...
}
在弹出窗口中:
var sharedObject = window.opener.sharedObject;
// function you have to to call to close the popup window
function myclose()
{
//Define the parameters you want to pass to the main calling window
sharedObject.var1 =
sharedObject.var2 =
...
window.opener.closePopupWindow();
}
就是这样!
这非常方便,因为:
玩得开心!