我有一个具有多种形式的MVC应用程序。提交表单后,它会将用户的gps坐标传递给服务器。我还想传递客户当前的日期时间。

有人对实现此目标的最佳方法有任何想法吗?

表单是标准的html表单,并使用基本提交。

我的表格看起来像下面的表格。那么,如何最好地将javascript datetime值附加到提交调用中?

            @using (Ajax.BeginForm("CheckIn", "Home", "Nothing", new AjaxOptions { }))
            {
                <fieldset>
                    <p>Move your Tile here to let those around you see it...</p>
                    <input type="hidden" name="lat" id="hckLat" value="" />
                    <input type="hidden" name="lon" id="hckLon" value="" />
                    <input type="hidden" name="date" id="hckDate" value="" />
                    <input type="submit" value="Check In!" />
                </fieldset>
            }

最佳答案

您可以使用客户端计算机上呈现的JavaScriptDate函数。

var d = new Date();

然后,您可以在提交坐标时将变量d传递给服务器。

根据您的编辑:

CheckinHome controller方法中,您可以指定:
public ActionResult Checkin(string hckLat, string hckLong, string hckDate)
{
    var theDate = hckDate;
}

要设置hckDate的值,您可以执行以下操作:

在提交按钮上,单击:
<input type="button" name="theSubmitButton" id="theSubmitButton" value="Button"   onClick="setDate();">

function setDate() {
   var e = document.getElementById('hckDate');

   e.value = new Date();
}

然后将其发送到服务器。

09-25 18:59
查看更多