通过查询字符串到MVC控制器传递对象的列表 [英] Passing List of objects via querystring to MVC Controller

查看:97
本文介绍了通过查询字符串到MVC控制器传递对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我需要的对象的列表传递到MVC控制器,但我不知道如何在查询字符串格式化这个。我之所以要这么做,这是因为这不是一个Web应用程序,它是通过查询字符串和其路由到一个控制器,做的工作接受数据的Web服务。

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. 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.

所以,对于一个类中调用MyParam具有属性A和B,我怎么能构建一个查询字符串,将数据传递给下面的控制器方法:

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:

public ActionResult MyMethod(List<MyParam> ParamList)

我使用MVC框架RedirectToAction和RedirectToResult,看看它出现试过,但我认为我与MVC n00bness导致我犯了一个错误,因为它永远不会正确地传递数据的MyMethod始终都是空为参数

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.

在此先感谢!

推荐答案

您可能会发现的下面的博客文章你需要,如果你想默认的模型绑定成功解析请求到对象的强类型数组用列表的传输格式有用。查询字符串的示例:

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. Example of query string:

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

其中:

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

将成功地绑定到:

will successfully bind to:

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

这篇关于通过查询字符串到MVC控制器传递对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆