admin管理员组文章数量:1287511
I have the following method in 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 ;
}
Method in C# controller looks like this:
public ActionResult report()
{
return View();
}
Now I need to pass parameters like name and date when the URL is accessed to the C# method. How can I do that?
I have the following method in 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 ;
}
Method in C# controller looks like this:
public ActionResult report()
{
return View();
}
Now I need to pass parameters like name and date when the URL is accessed to the C# method. How can I do that?
Share Improve this question edited Apr 6, 2017 at 9:00 Manfred Radlwimmer 13.4k13 gold badges55 silver badges64 bronze badges asked Apr 29, 2013 at 10:51 NarutoNaruto 9,63439 gold badges119 silver badges204 bronze badges2 Answers
Reset to default 3Append the parameters to the URL in the following format:
<url>?param1=value1¶m2=value...
If you need to pass an array of values then use the same name for the parameter
<url>?arr=value1&arr=value2...
So your URL would look like
domain./Controller/Report?name=xyz&date=20
Update:
To receive them in the Action, declare the parameters being passed to the Action.
public ActionResult Report(string name, DateTime date)
{
...
}
Read up on Model Binding in ASP.NET MVC
I found a better way to pass parameters to the popup window and even to retrieve parameters from it :
In the main page :
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;
...
}
In the popup window :
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();
}
That's it !
And this is very convenient because:
- You have not to set parameters in the URL of the popup window.
- No form to define
- You can use illimited parameters even objects.
- Bi-directionnal : you can pass parameters AND, if you want you, can retreive new parameters.
- Very easy to implement.
Have Fun!
本文标签: javascriptHow to pass parameter in windowopen methodStack Overflow
版权声明:本文标题:javascript - How to pass parameter in window.open method? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741250383a2365680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论