javascript - 如何在 ASP.net 应用程序中获取用户时区偏移并将其发送到服务器

标签 javascript asp.net asp.net-mvc asp.net-mvc-5 timezone

当用户成功登录时,我对如何捕获/发送用户的时区/偏移感到困惑。在互联网上,我找到了“getTimezoneOffset”来完成工作。但我不知道如何发送在 javascript 中隐式计算且未由用户在登录表单中显式填写的数据。我是否需要一些隐藏的文件或触发 javascript 的东西,然后捕获它的值,我可以将其发送回服务器。请指导我。以下是我到目前为止所拥有的

CSHTML 文件:

@model LoginMV
@using SIM_Obj.ModelViews;

@{ 
    ViewBag.Title = "Tenant Login";
}

@using (Html.BeginForm("LoginSubmit", "Security", FormMethod.Post, new { id = "simLogin" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal-width-inherit">
        @Html.HiddenFor(model => model.ReturnUrl)

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
            @Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label ignoreSpacing col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Username, new { @class = "form-control" } )
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label ignoreSpacing col-md-2" })
            <div class="col-md-10">
                @Html.PasswordFor(model => model.Password, new { @class = "form-control" } )
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Login" class="btn btn-default" />
            </div>
        </div>
    </div>
}

View 模型:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LoginSubmit(LoginMV userData)
{
    if (userData != null && !string.IsNullOrEmpty(userData.Username) && !string.IsNullOrEmpty(userData.Password))
    {
        UserMV authenticatedUser = SecurityHelper.AuthenticateTenantUser(userData.Username, userData.Password);

        if (authenticatedUser == null)
        { ModelState.AddModelError("Username", "Incorrect"); }
        else
        {
            SimUtils.CurrentTenantId = authenticatedUser.Id;
            SimUtils.CurrentAccountId = authenticatedUser.AccountId;
        }
    }

    return View("Login", userData);
}

最佳答案

将用户当前日期/时间传递到服务器的最简单方法是使用 Razor 表单上的隐藏字段将额外数据发送到 Controller 。

做这样的事情。

在您看来,添加:

@Html.Hidden('UserLogInDate', null, new {id = "hidUserLoginDate"})

这将在您的 View 上创建隐藏字段,并为其指定指定的 ID。

那么您可能(应该)有一个单独的 JavaScript 文件用于登录 View 。在其中,您可以在用户打开 View 时使用客户端日期/时间填充隐藏字段。

然后您需要在模型中包含该字段,例如:

public datetime UserLogInDate {get; set;}

这样您就可以处理日期/时间并在 Controller 中获取时区。

关于javascript - 如何在 ASP.net 应用程序中获取用户时区偏移并将其发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40510963/

相关文章:

javascript - 在 $watch 回调中使用 $scope 有意义吗?

javascript - IE7 中的圆形悬停区域

c# - 回发后如何检查表单

jquery - 如何将 ViewBag 值传递给 razor 中的 JavaScript 函数?

asp.net-mvc - 当我指定某些文件扩展名时,ASP.net 路由被忽略

javascript - ion-toggle ng-change 无法通过 Controller + 服务结构正确触发

javascript - 打开分页时如何从(html) Bootstrap 表中获取完整数据

c# - 如何使 Global.asax 引用 aspx 中声明的下拉列表?

asp.net - 是否有任何算法可以检查正确的 NPI(国家提供商识别)号码?

asp.net-mvc - IIS服务器和Windows文件系统软链接(soft link)