C# 通过查询字符串传递数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14596862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing array via query string
提问by SHAKEEL HUSSAIN
I am passing a javascript array via Request.QueryString["cityID"].ToString()
, but it shows me an error of "invalid arguments":
我正在通过 传递一个 javascript 数组Request.QueryString["cityID"].ToString()
,但它向我显示了“无效参数”的错误:
blObj.addOfficeOnlyDiffrent(Request.QueryString["cityID"].ToString(),
Request.QueryString["selectTxtOfficeAr"].ToString());
The method declaration looks like:
方法声明如下所示:
public string addOfficeOnlyDiffrent(string[] cityID, string selectTxtOfficeAr) { }
采纳答案by Habib
Your method addOfficeOnlyDiffrent
is expecting a string array arguement in cityID
whereas you are passing a single string
type object to your method in call. I believe your cityID
is a single string so you can remove the array from the method declaration. In your method call.
您的方法addOfficeOnlyDiffrent
需要一个字符串数组参数,cityID
而您string
在调用中将单个类型对象传递给您的方法。我相信您cityID
是一个字符串,因此您可以从方法声明中删除该数组。在你的方法调用中。
Request.QueryString["cityID"].ToString()
the above represents a single string, not a string array.
以上表示单个字符串,而不是字符串数组。
If your query string contains a string array, then values are probably in string representation, separated by some character, for example ,
. To pass that string to the method, you can call string.Split
to split the string to get an array.
如果您的查询字符串包含字符串数组,则值可能采用字符串表示形式,由某些字符分隔,例如,
. 要将该字符串传递给该方法,您可以调用string.Split
拆分字符串以获取数组。
EDIT: From your comment, that your query string contains:
编辑:根据您的评论,您的查询字符串包含:
Request.QueryString["cityID"].ToString() (123456,654311,987654)
You can do the following.
您可以执行以下操作。
string str = Request.QueryString["cityID"].ToString();
string[] array = str.Trim('(',')').Split(',');
blObj.addOfficeOnlyDiffrent(array,
Request.QueryString["selectTxtOfficeAr"].ToString());
回答by Tim Medora
If your query string parameter value looks like: "123456,654311,987654"
如果您的查询字符串参数值类似于:“123456,654311,987654”
string[] ids = Request.QueryString["cityID"]
.Split( new[] {","}, StringSplitOptions.RemoveEmptyEntries );
回答by Ajay
try this it's so simple.
试试这个就这么简单。
string city = Request.QueryString["cityID"];
string[] cityID =city.Split(new char[] {','},StringSplitOptions.RemoveEmptyEntries);
You get string like this "123456,654311,987654" in city. You get array in cityID.
你会在city 中得到这样的字符串 "123456,654311,987654" 。您在 cityID 中获得数组。