简体   繁体   中英

How to store all the form posted data in asp.net dictionary

I am using c# as code behind.

I have got lots of values which is been posted when my FORM is submitted, below are few of them:

skywardsNumber  99999039t
password    a2222222
ctl00$MainContent$ctl22$FlightSchedules1$ddlDepartureAirport-suggest    Alice Springs (ASP)
ctl00$MainContent$ctl22$ctl07$txtPromoCode  ManojPromo

Now I want to store all the FORM posted values in the asp.net dictionary and then that dictionary object will be saved in SESSIONS for further use.

Please suggest how can I store the FORM POSTED values in asp.net DICTIONARY .

Try this approach:

using System.Linq;
using System.Collections;
using System.Collections.Generic;
...

Dictionary<string, string> form = new Dictionary<string, string>(from key in Request.Form.AllKeys select new DictionaryEntry(key, Request.Form[key]));
Session["MyKey"] = form;

UPDATE

Without LINQ:

Dictionary<string, string> form = new Dictionary<string, string>();
foreach(string key in Request.Form.AllKeys)
    form.Add(key, Request.Form[key]);
Session["MyKey"] = form;

You could create a class that implements IDictionary :

public class RequestDictionary : IDictionary<string, string>
{


    private HttpRequest request;
    public RequestDictionary(HttpRequest request)
    {
        this.request = request;
    }


    public string this[string key] {
        get { return request(key); }
        set {
            throw new NotImplementedException();
        }
    }

    // ...

}

You can then create an RequestDictionary object from you request and use it like a dictionary, without copying all you values in a new dictionary.

You should be able to do the following.

Request["varaibleName"];

or to loop through

 foreach ( string var in Request.Form ) {
     Response.Write ( Request.Form[ var ] );
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM