asp.net-mvc - 使用 Viewdata 在 Html.Label 中显示 IP 地址
问题描述
我正在尝试使用将 IP 地址传递给我的视图,ViewData
但它显示不正确。以下是我的标签编码:
@Html.Label(ViewData["CREATED_LOC"].ToString(), new { @class = "form-control", @disabled = "disabled" })
我在我的模型中进行了检查,获取的数据在字符串“10.200.4.7”中,但在我看来它只显示 7。我可以知道是什么问题吗?
解决方案
您可以在 c# 控制器中使用 ViewBag ... ViewBag:
ViewBag.IpAddress = Request.UserHostAddress;
之后你可以像这样在视图中使用(显示:无供您选择):
<span id="spnIpAddress" style="display:none">@ViewBag.IpAddress</span>
如果您可以在 ajax 请求中使用此 IP 地址。你可以这样使用:
_ipAddress: $('#spnIpAddress').text(),
推荐阅读
- c - 在非常基本的 C 程序上使用 GDB 进行温度转换时出现错误,指出“printf.c:没有这样的文件或目录”
- rest - 如何通过邮递员使用休息电话在 SharePoint 2019 上创建带有 % 字符的文件夹?
- hadoop - HDFS:仅删除一个节点的数据节点目录
- amazon-web-services - 如何为 AWS Appsync 编写单元测试用例
- php - Chartjs 与 PHP 问题(意外的令牌)
- css - 为什么每个@font-face 都有不同的字体文件?
- python - 计算同一列之间的差异,在python中由另一列分组的连续行
- sql-server - 将新行更改为新记录
- assembly - (如果 BYTE PTR [ebp+0x8] 得到一个大于 1 字节的参数会怎样?比如 0xcdc485c1
- python - 使用python和django测试参考值