首页 > 解决方案 > 使用 Viewdata 在 Html.Label 中显示 IP 地址

问题描述

我正在尝试使用将 IP 地址传递给我的视图,ViewData但它显示不正确。以下是我的标签编码:

@Html.Label(ViewData["CREATED_LOC"].ToString(), new { @class = "form-control", @disabled = "disabled" })

我在我的模型中进行了检查,获取的数据在字符串“10.200.4.7”中,但在我看来它只显示 7。我可以知道是什么问题吗?

标签: asp.net-mvcrazorhtml-helper

解决方案


您可以在 c# 控制器中使用 ViewBag ... ViewBag:

ViewBag.IpAddress = Request.UserHostAddress;

之后你可以像这样在视图中使用(显示:无供您选择):

<span id="spnIpAddress" style="display:none">@ViewBag.IpAddress</span>

如果您可以在 ajax 请求中使用此 IP 地址。你可以这样使用:

_ipAddress: $('#spnIpAddress').text(),

推荐阅读