简体   繁体   English

从HttpContext.request对象获取列表

[英]Get a list from an HttpContext.request object

Using jQuery I have serialized a form that and send it to the server in this format: 我使用jQuery序列化了一个表单,并以这种格式将其发送到服务器:

Object{
transactionID : "10779"
itemList : [{itemName:"ball", quantity: 5}, {itemName:"stuff", quantity:10}]
}

In a custom ASP.NET modelbinder, I do this: 在自定义ASP.NET modelbinder中,我这样做:

HttpRequestBase request = controllerContext.HttpContext.Request;
        List<Item> itemList = new List<Item>();

        foreach (var item in request.Form.Get("itemList"))
        {
            itemList.Add(new TransactionItemQuantity
            {
               name = item.itemName               
               quantity = item.quantity
            });
        }

        return new Transaction
        {
             transactionID = request.Form.Get("transactionTypeID"),
             itemList = itemList
        };
    }

However, the foreach loop does not work, as the IDE doesn't yet know that request.Form.Get("itemList") returns an array of objects. 但是,由于IDE尚不知道request.Form.Get(“ itemList”)返回对象数组,因此foreach循环不起作用。 How do I make the above code work? 如何使以上代码起作用?

If you do it like this you only get a char-array back from the request. 如果这样做,您只会从请求中获得一个char数组。 You need to deserialize the content of request.Form.Get("itemList") into a List of your items, then you can loop through them. 您需要将request.Form.Get("itemList")的内容反序列化为项目列表,然后可以遍历它们。 Something like this: 像这样:

var list = JsonConvert.DeserializeObject<List<Item>>(request.Form.Get("itemList"));

You are also creating a list of type Item first, but trying to add objects of type TransactionItemQuantity in the loop. 您还将首先创建Item类型的列表,但尝试在循环中添加TransactionItemQuantity类型的对象。

Edit: Added example 编辑:添加示例

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

相关问题 将 HttpContext.Request 作为对象读取? - Reading HttpContext.Request as object? HttpContext.Request从listBox的项目? - HttpContext.Request the items from listBox? HttpContext.Request [“ x”]引发异常 - HttpContext.Request[“x”] throws exception 使用最小起订量填充httpcontext.request - using moq to populate httpcontext.request HttpContext.Request或HttpContext.Current.Request URL中是否缺少“#”字母? - '#' letter is missing in HttpContext.Request or HttpContext.Current.Request url? C# 非静态字段、方法或属性“HttpContext.Request”需要对象引用 - C# An object reference is required for the non-static field, method, or property 'HttpContext.Request' 从 ASP.NET Core HttpContext.Request 的 multipart/form-data 内容读取 excel 文件? - Reading excel file from ASP.NET Core HttpContext.Request of multipart/form-data content? HttpContext.Request 和 Request a .NET Core controller 有什么区别? - What is the Difference between HttpContext.Request and Request a .NET Core controller? 传递HttpContext.Request参数或使用静态实例? - Pass HttpContext.Request parameter around or use static instance? 调用HttpContext.Request时如何避免HttpException? - How to avoid a HttpException when calling HttpContext.Request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM