0

我有一个帖子,其中包含一组项目,其中没有键的 int 而是键的字符串。例如:(来自帖子)

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

所以“pool”是一个数组,其键设置为名称,其值设置为字符串。

从基本上看起来像这样的形式

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

我尝试了几种方法来提取信息,但缺点是我必须能够知道关键是什么,这样我才能测试它,这样我就知道我得到了价值。我对发布数据没有选择权,就这样被卡住了。我不能使用 .ToDictionary 之类的东西,因为我也被困在 .net 2.0 中。

这是通过 KeyValuePair 获取正确信息的尝试,你是的,这是错误的,但至少我希望它能够给出需要发生的事情的想法。

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);

有人对如何使这项工作有想法吗?我认为将 String[] 转换为 Dictionary 或 Hashtable 将是诀窍,但是.. 到目前为止它还没有工作。

感谢您的帮助。干杯,杰里米巴斯

旁注 如果这是 php,我只需要去

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

如果可以解决一些问题,我将拥有所需的价值。

4

2 回答 2

0

输入会误导您。您实际上正在获取属性 [对象] = 值。如果您使用 Key = some_function(object,property), Value=value 创建 KVP,您将朝着正确的方向前进。例如,作为上述代码的一次性:

 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....
于 2012-06-01T14:46:14.640 回答
0

解决方案是这样的

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

简而言之,您需要直接针对第一级表单键,因为 C# .net 2.0 至少不明白来自帖子的数组可以具有除 int 之外的键。当它看到这一点时,它所做的就是将其视为一个字符串,因此,您将其称为一个字符串。

谢谢你们的帮助。干杯-杰里米

于 2012-06-01T17:28:46.353 回答