ASP.Net MVC:在单个参数中提交数组/集合
是否可以(以及如何)发送带有存储在单个参数中的数组的 post 请求?喜欢
myStringArray=你好,世界
以及接受此参数作为数组的操作,以 ,
作为分隔符,
public ActionResult MyAction(string[] myStringArray)
{
//myStringArray[0] == "hello" and myStringArray[1] == "world"
}
参数 myStringArray 的格式并不重要。但它必须是单个参数。
谢谢
Is it possible (and how) to send a post request with an array stored in a single parameter? like
myStringArray=hello,world
and an action that accepts this parameter as an array with ,
as separator
public ActionResult MyAction(string[] myStringArray)
{
//myStringArray[0] == "hello" and myStringArray[1] == "world"
}
the format of the parameter myStringArray doesn't matter. But it has to be a single parameter.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
取决于您如何将数据发送到服务器。如果您从 url 参数或普通文本框执行此操作,其中包含以下数据:
那么您不需要数组参数,只需用逗号将字符串拆分为数组即可:
但如果您通过 AJAX 发送此内容,您也可以直接发送它。如果你想发送一个真实的数组,那么你的javascript应该如下所示 答案。
Depends on how you are sending the data to the server. If you are doing this from a url param or normal textbox which has data like:
Then you dont need an array parameter, just split the string by commas into an array:
But if you are sending this by AJAX, you also send it directly. If you want to send a real array, then your javascript should look like this answer.
这是我在这个场景中使用的 IModelBinder。
Here is an IModelBinder that I've been using for this scenario.
如果您创建一个包含数组的模型,那么应该没有问题。当然,您需要一个能够利用该强类型的视图(单击“创建强类型视图”并在列表中找到您刚刚创建的视图模型),您应该没有问题。
If you create a model with an array in it you should have no trouble. Then of course you nee a view that will utilize that strong type (click "Create a strongly-typed view" and find your view-model you just created in the list) you should have no problem.