Alex
Alex

Reputation: 9720

Add to dictionary some params from request

I want to get some params from Request

I need from Request.Params all params with text contains "txt" I have more type of text structure:

"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtPhone"
"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtPhone2"
"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtPhone3"
"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtAdr1"
"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtAdr2"
"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtAdr3"

how Get value all text after "txt"

var dictionary =  new Dictionary<string, string>();

 foreach (var key in Request.Params.AllKeys)
 { 
      if (key.ToString().Contains("txt"))
                {
                 // add to dictionary name and value
                // dictionary.Add("name", "val");
                }
 }

Upvotes: 0

Views: 153

Answers (3)

Justin Harvey
Justin Harvey

Reputation: 14672

var dictionary =  new Dictionary<string, string>();

 foreach (var key in Request.Params.AllKeys)
 { 
      if (key.ToString().Contains("txt"))
      {
         // add to dictionary name and value
         dictionary.Add(key.Split(new string[]{"txt"}, StringSplitOptions.None)[1], Request.Params[key]);
      }
 }

Upvotes: -1

DLeh
DLeh

Reputation: 24395

Are you asking how to add to the dictionary?

var dictionary =  new Dictionary<string, string>();
foreach (var key in Request.Params.AllKeys)
{ 
     if (key.ToString().Contains("txt"))
     {
          //get the text after "txt"
          var index = Request.Params[key].LastIndexOf("txt");
          var val = Request.Params[key].SubString(index);
          Dictionary.Add(key, val);
     }
}

Upvotes: 1

Amit Joki
Amit Joki

Reputation: 59232

You can do this:

var dictionary =  new Dictionary<string, string>();
foreach (var key in Request.Params.AllKeys)
{ 
     if (key.ToString().Contains("txt"))
     {
          int index = Request.Params[key].LastIndexOf("txt");
          Dictionary.Add(key, Request.Params[key].SubString(index));
     }
}

Upvotes: 2

Related Questions