简体   繁体   中英

String[] from post to Dictionary or Hashtable in C# .net 2.0

I have a post that has an array of items coming in that have no int for keys but a string for a key. for example: (from post)

-----------------------------3805226668837
Content-Disposition: form-data; name="pool[Abelson_2834.jpg]"

general_pool
-----------------------------3805226668837
Content-Disposition: form-data; name="pool_place[Abelson_2834.jpg]"

1
-----------------------------3805226668837
Content-Disposition: form-data; name="mediatype[Abelson_2834.jpg]"

3
-----------------------------3805226668837
Content-Disposition: form-data; name="pool[Abelson_2852.jpg]"

general_pool
-----------------------------3805226668837
Content-Disposition: form-data; name="pool_place[Abelson_2852.jpg]"

1
-----------------------------3805226668837
Content-Disposition: form-data; name="mediatype[Abelson_2852.jpg]"

3
-----------------------------3805226668837
Content-Disposition: form-data; name="pool[Abelson_3160-1.jpg]"

general_pool
-----------------------------3805226668837
Content-Disposition: form-data; name="pool_place[Abelson_3160-1.jpg]"

1
-----------------------------3805226668837
Content-Disposition: form-data; name="mediatype[Abelson_3160-1.jpg]"

3
-----------------------------3805226668837
Content-Disposition: form-data; name="files[]"; filename="Abelson_2834.jpg"
Content-Type: image/jpeg

So "pool" is an array with the keys set as the name and it's value is set aa a string.

from the form that lookes like this basicly

<tbody data-target="#modal-gallery" data-toggle="modal-gallery" class="files">
  <tr class="template-upload fade in">
    <td class="preview"><span class="fade in">
      <canvas width="80" height="54"></canvas>
      </span></td>
    <td class="name"><span>Abelson_2834.jpg</span></td>
    <td class="size"><span>4.17 MB</span></td>
    <td><div aria-valuenow="0" aria-valuemax="100" aria-valuemin="0" role="progressbar" class="progress progress-success progress-striped active">
        <div style="width:0%;" class="bar"></div>
      </div>
      <div="forminputs"> <span style="display:inline-block;float:left;">Choose a pool:
      <select style="display:inline-block;" name="pool[Abelson_2834.jpg]">
        <option selected="selected" value="general_pool">General</option>
        <option value="place">Places</option>
        <option value="geo">Geometrics</option>
        <option value="ads">Advertisment</option>
      </select>
      <span class="pool_place"> <br>
      Choose a pool:
      <select style="display:inline-block;" name="pool_place[Abelson_2834.jpg]">
        <option selected="selected" value="1">place 1</option>
        <option value="2">Places</option>
        <option value="3">Geometrics</option>
        <option value="4">Advertisment</option>
      </select>
      </span> </span> <span style="display:inline-block;float:left;">Choose Media type:
      <select style="display:inline-block;" name="mediatype[Abelson_2834.jpg]">
        <option value="1">marker_icon</option>
        <option value="2">user_image</option>
        <option selected="selected" value="3">general_image</option>
        <option value="4">general_video</option>
        <option value="5">google_static_map</option>
      </select>
      </span> </div="forminputs"></td>
    <td class="start"><button class="btn btn-primary"> <i class="icon-upload icon-white"></i> <span>Start</span> </button></td>
    <td class="cancel"><button class="btn btn-warning"> <i class="icon-ban-circle icon-white"></i> <span>Cancel</span> </button></td>
  </tr>
  <tr class="template-upload fade in">
    <td class="preview"><span class="fade in">
      <canvas width="80" height="54"></canvas>
      </span></td>
    <td class="name"><span>Abelson_2852.jpg</span></td>
    <td class="size"><span>3.17 MB</span></td>
    <td><div aria-valuenow="0" aria-valuemax="100" aria-valuemin="0" role="progressbar" class="progress progress-success progress-striped active">
        <div style="width:0%;" class="bar"></div>
      </div>
      <div="forminputs"> <span style="display:inline-block;float:left;">Choose a pool:
      <select style="display:inline-block;" name="pool[Abelson_2852.jpg]">
        <option selected="selected" value="general_pool">General</option>
        <option value="place">Places</option>
        <option value="geo">Geometrics</option>
        <option value="ads">Advertisment</option>
      </select>
      <span class="pool_place"> <br>
      Choose a pool:
      <select style="display:inline-block;" name="pool_place[Abelson_2852.jpg]">
        <option selected="selected" value="1">place 1</option>
        <option value="2">Places</option>
        <option value="3">Geometrics</option>
        <option value="4">Advertisment</option>
      </select>
      </span> </span> <span style="display:inline-block;float:left;">Choose Media type:
      <select style="display:inline-block;" name="mediatype[Abelson_2852.jpg]">
        <option value="1">marker_icon</option>
        <option value="2">user_image</option>
        <option selected="selected" value="3">general_image</option>
        <option value="4">general_video</option>
        <option value="5">google_static_map</option>
      </select>
      </span> </div="forminputs"></td>
    <td class="start"><button class="btn btn-primary"> <i class="icon-upload icon-white"></i> <span>Start</span> </button></td>
    <td class="cancel"><button class="btn btn-warning"> <i class="icon-ban-circle icon-white"></i> <span>Cancel</span> </button></td>
  </tr>
  <tr class="template-upload fade in">
    <td class="preview"><span class="fade in">
      <canvas width="80" height="54"></canvas>
      </span></td>
    <td class="name"><span>Abelson_3160-1.jpg</span></td>
    <td class="size"><span>3.00 MB</span></td>
    <td><div aria-valuenow="0" aria-valuemax="100" aria-valuemin="0" role="progressbar" class="progress progress-success progress-striped active">
        <div style="width:0%;" class="bar"></div>
      </div>
      <div="forminputs"> <span style="display:inline-block;float:left;">Choose a pool:
      <select style="display:inline-block;" name="pool[Abelson_3160-1.jpg]">
        <option selected="selected" value="general_pool">General</option>
        <option value="place">Places</option>
        <option value="geo">Geometrics</option>
        <option value="ads">Advertisment</option>
      </select>
      <span class="pool_place"> <br>
      Choose a pool:
      <select style="display:inline-block;" name="pool_place[Abelson_3160-1.jpg]">
        <option selected="selected" value="1">place 1</option>
        <option value="2">Places</option>
        <option value="3">Geometrics</option>
        <option value="4">Advertisment</option>
      </select>
      </span> </span> <span style="display:inline-block;float:left;">Choose Media type:
      <select style="display:inline-block;" name="mediatype[Abelson_3160-1.jpg]">
        <option value="1">marker_icon</option>
        <option value="2">user_image</option>
        <option selected="selected" value="3">general_image</option>
        <option value="4">general_video</option>
        <option value="5">google_static_map</option>
      </select>
      </span> </div="forminputs"></td>
    <td class="start"><button class="btn btn-primary"> <i class="icon-upload icon-white"></i> <span>Start</span> </button></td>
    <td class="cancel"><button class="btn btn-warning"> <i class="icon-ban-circle icon-white"></i> <span>Cancel</span> </button></td>
  </tr>
</tbody>

I have tried a few ways to pull out the information but the short of it is that I have to be able to know what the key is so I can test it so I know I'm getting the value. I don't have a choice on the post data, that is stuck the way it is. I can't use things like .ToDictionary cause I'm also stuck in .net 2.0.

Here is an attempt at getting the right info via just KeyValuePair thou yes it's wrong but at least I hope it'll give the idea of what is needing to happen.

media.file_name = "Abelson_2834.jpg"; // test value to pin to
int type = 3;
// NOTE I KNOW KeyValuePair IS NOT RIGHT.. looking for right way thou
foreach (KeyValuePair<string, string> obj in Request.Params["mediatype"])
{
    if( obj.Key == media.file_name ){
        type =  int.Parse(obj.Value); 
        // value of type should be 3 since there is a 
        // Request.Params["mediatype"]["Abelson_2834.jpg"] but since you can't call keys
        // by name it thnk it should be an int
        // like Request.Params["mediatype"][0]
    }
}

media.type = ActiveRecordBase<media_types>.Find(type);
ActiveRecordMediator<media_repo>.Save(media);

Anyone have an idea on how to make this work? I would think that turning the String[] to a Dictionary or Hashtable would be the trick but.. as of yet it's not working.

Thank you for the help. Cheers, jeremyBass

SIDE NOTE If this was php, I'd just have to go

$value = $_POST["mediatype"]["Abelson_2834.jpg"];

and I'd have the value needed if that clears things up some.

The input is misleading you. You actually are getting property[object]=value. If you create KVPs with Key = some_function(object,property), Value=value you'll be headed in the right direction. For example, as a one-off of your code above:

 if (!String.IsNullOrEmpty(Request.Params["mediatype"])){
        foreach (KeyValuePair<string, string> obj in Request.Params["mediatype"])
        {
           dict_props[obj.Key+"_mediatype"]=value;
           // add each object to a list
        }
 }
 // for each object in the list
 //    for each property
 ...process dictionary....

The solution is this

media.file_name = "Abelson_2834.jpg";
int type = 3;
int tmp = int.Parse(Request.Params["mediatype[" + media.file_name+ "]"]);
if(tmp>0){
    type = tmp;
}

In short you need to target directly at the first level form keys as C# .net 2.0 at least doesn't understand that an array when coming from a post can have a key other then a int. When it sees this what it does is treat it as a string so, you call it as one.

Thanks for the help you guys. Cheers -Jeremy

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