简体   繁体   English

通过查询字符串将对象列表传递给 MVC Controller

[英]Passing List of objects via querystring to MVC Controller

I've got a situation where I need to pass a List of objects to an MVC Controller, but I'm not sure how to format this in the querystring.我有一种情况,我需要将对象列表传递给 MVC Controller,但我不确定如何在查询字符串中格式化它。 The reason I would want to do this is because this is not a web application, it's a web service that accepts data via the querystring and routes it to a controller that does the work.我想这样做的原因是因为这不是 web 应用程序,而是 web 服务,它通过查询字符串接受数据并将其路由到有效的 controller 服务。

So, given a class called MyParam with properties A & B, how can I construct a querystring that will pass data to the following controller method:因此,给定一个名为 MyParam 且具有属性 A 和 B 的 class,我如何构造一个查询字符串,将数据传递给以下 controller 方法:

public ActionResult MyMethod(List<MyParam> ParamList)

I've tried using the MVC framework to RedirectToAction and RedirectToResult to see what it comes up with, but I assume that my n00bness with MVC is causing me to make a mistake because it never passes the data correctly and MyMethod always has null for the parameter.我已经尝试使用 MVC 框架来 RedirectToAction 和 RedirectToResult 来查看它的结果,但我认为我对 MVC 的 n00bness 导致我犯了一个错误,因为它永远不会正确传递数据并且 MyMethod 总是有 null 作为参数.

Thanks in advance!提前致谢!

You may find the following blog post useful for the wire format of lists you need to use if you want the default model binder to successfully parse the request into a strongly typed array of objects.如果您希望默认的 model 绑定器成功地将请求解析为强类型的对象数组,您可能会发现以下博客文章对于您需要使用的列表的有线格式很有用。 Example of query string:查询字符串示例:

[0].Title=foo&[0].Author=bar&[1].Title=baz&[1].Author=pub...

where:在哪里:

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
}

will successfully bind to:将成功绑定到:

public ActionResult MyMethod(IEnumerable<Book> books) { ... }

Ok, based on the information provided I don't think you want what you think you want.好的,根据提供的信息,我不认为你想要你想要的。 In your case on the client you POST the data to the controller.在您的客户端上,您将数据发布到 controller。 Not use a querystring.不使用查询字符串。

ok since you have to use querystring.好的,因为您必须使用查询字符串。 my new answer: serialize object, convert it to base64 string and pass it, then convert it back.我的新答案:序列化 object,将其转换为 base64 字符串并传递它,然后将其转换回来。

I've found that the JsonValueProvider works much better than the normal ValueProvider for binding to a list.我发现 JsonValueProvider 在绑定到列表方面比普通的 ValueProvider 工作得更好。 Simply convert your data to a JSON object like so:只需将您的数据转换为 JSON object,如下所示:

<YourRoute>?ParamList=[{SomeProperty:'Value'},{SomeProperty:'Value'}];

And the JsonValueProvider will take care of the rest. JsonValueProvider 将负责 rest。 This is assuming you have the ability to say "post this data as Json".这是假设您有能力说“将此数据发布为 Json”。

I also disclaim whether or not this would be a good idea.我也否认这是否是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将对象列表传递给 Controller - Passing List of Objects to Controller 使用 jQuery Ajax 将对象列表传递给 MVC 控制器方法 - Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax 使用jQuery Ajax将对象列表传递到ActionResult MVC控制器方法 - Passing A List Of Objects Into An ActionResult MVC Controller Method Using jQuery Ajax MVC actionlink传递对象列表 - MVC actionlink passing list of objects MVC通过$ .ajax将文件传递给控制器 - MVC passing a file to a controller via $.ajax 是否可以在MVC控制器中附加查询字符串 - Is that possible to append the querystring in the controller for MVC 在 MVC 控制器中使用查询字符串变量 - Use querystring variables in MVC controller 使用 Html.BeginForm() ASP.NET MVC 将对象列表从控制器传递到视图时出错 - Error when passing list of objects from Controller to View using Html.BeginForm() ASP.NET MVC 将值从QueryString传递到ASP.NET MVC中的控制器的Index操作 - Passing value from QueryString into Index action of controller in asp.net mvc Web API 2 / MVC 5:属性路由将参数作为查询字符串传递,以在同一控制器上定位不同的操作 - Web API 2 / MVC 5 : Attribute Routing passing parameters as querystring to target different actions on same controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM