HTML Textarea输入含&的内容无法完整显示的技术求助
解决textarea输入含&内容被截断的问题
你的问题是textarea输入类似testing123!&aaa的内容后,仅返回testing123!,这是因为表单默认的application/x-www-form-urlencoded编码会把&识别为参数分隔符,导致服务器只接收到&之前的部分。以下是几种解决办法:
修改表单编码类型
在表单标签中添加enctype="multipart/form-data",这样表单数据会以二进制形式提交,&不会被当作参数分隔符:<form method="post" enctype="multipart/form-data"> <span class="value"> @Html.TextAreaFor(m => m.Nr2FieldComment, 4, 43, new { @class = "" }) </span> <!-- 其他表单元素 --> <button type="submit">提交</button> </form>前端URL编码处理
在表单提交前,用JavaScript把textarea内容中的特殊字符(包括&)转成URL编码格式,服务器端会自动解码:// 监听表单提交事件 document.querySelector('form').addEventListener('submit', function(e) { const commentTextarea = document.getElementById('Nr2FieldComment'); if (commentTextarea) { // 对内容进行URL编码 commentTextarea.value = encodeURIComponent(commentTextarea.value); } });后端确保正确读取值
如果是手动读取表单数据,不要拆分Request.QueryString或Request.Form,直接通过键名获取完整值:string comment = Request.Form["Nr2FieldComment"];ASP.NET的模型绑定默认会正确处理编码后的参数,只要前端提交的编码正确,模型
m.Nr2FieldComment就能获取到完整内容。
内容的提问来源于stack exchange,提问作者Ahmed Nassef
相关产品推荐
相关产品推荐

