7

目前,我有一组使用相同name属性动态创建的文本框:

<input type="text" name="SameName" value="Value1" />
<input type="text" name="SameName" value="Value2" />

在服务器端,我收到提交的表单(POST)并访问Request.Form["SameName"],值为Value1,Value2.

我的问题是,是否可以以某种方式将分隔符从逗号更改为管道(或其他字符)?

我不能只用管道替换逗号,因为我需要分隔不同的字段:

<input type="text" name="SameName" value="Val,ue1" />
<input type="text" name="SameName" value="Value2" />

将会:

价值,ue1,价值2

建议我有 3 个文本字段而不是两个。所以简单Replace(',','|')是没有帮助的。

4

3 回答 3

14

In reality, the POST sends the values of the two inputs individually. You're seeing the concatenated version because of how you're accessing it from Request.Form (which is a NameValueCollection).

To be able to differentiate between the different POSTed values, you can use GetValues()

string[] values = Request.Form.GetValues("SameName");
于 2013-02-21T21:44:07.233 回答
1

如果它是一个字符串,你可以使用 string.replace 方法并告诉它你想要替换的字符,以及你想要用什么来替换它。

string sr = "Value1,Value2";
sr.Replace(",","|");

编辑

我认为您可以将 request.form 的返回分配给一个数组,然后遍历该数组以单独获取值并使用它们执行您想要的操作。这将解决结果中有逗号的问题。

于 2013-02-21T21:28:06.543 回答
-1

这是您尝试做的一个很好的例子:

public function MyAction(FormCollection form) as String
    dim sb = new Text.StringBuilder
    for each value as string in form.Getvalues("SameName")
        sb.Append(Server.UrlEncode(value) & ",")

    next
    'Remove trailing comma if you want
    If Right(sb.ToString, 1) = "," Then
        retVal.Remove(sb.Length - 1, 1)
    End If

'Do other fun stuff here if you want

    return sb.ToString

End Function

参考资料:ASP.NET MVC:检索同名表单字段- C# 中的多个同名字段

http://msdn.microsoft.com/en-us/library/zttxte6w.aspx - URL 编码功能

于 2013-02-21T21:57:02.640 回答