2

我有一个函数可以根据 dropdown 的变化在文本框中设置更新日期时间。

我的jQuery:

function ModifymyDiv (element) {

    var option = $("#myDiv option:selected").text();
    var requrl = '@Url.Action("PopulateConfirmationFields", "Controller", null, Request.Url.Scheme, null)';
    $.ajax({
        type: "Get",
        url: requrl,

        success: function (data) {

            $('#txtconfTime').empty();
            $('#txtconfName').empty();
            $('#txtconfName').val(data.name);
            $('#txtconfTime').val(data.time);
        },
        error: function (failure) {
        }
    });
}

我的 Json 方法:

 public ActionResult PopulateConfirmationFields()
        {
            User usr = userProxy.GetUserById(UserAppContext.UserOID);
            string name = string.Format("{1},{0}", usr.FirstName, usr.LastName);
            DateTime time = DateTime.Now;
            var t = new { name = name, time = time };
            return Json(t, JsonRequestBehavior.AllowGet);
        }

还在我的文本框中...

@Html.TextBoxFor(model => model.confirmationDateTime, new { id = "txtconfTime" })

DateTime 值看起来像...

/Date(1348040819674)/

如何将正确的日期时间格式传递给我的 TextBox ?PS 在这种特殊情况下,不能将其修改为字符串。

4

2 回答 2

1

找到解决方案:

        var jsonDate= data.time;
        var date = new Date(parseInt(jsonDate.substr(6)));
        var curr_date = date.getDate();
        var curr_month = date.getMonth();
        curr_month++; //January is represented by 0 
        var curr_year = date.getFullYear();
        var date =curr_month + "/" + curr_date + "/" + curr_year;
            $('#txtconfTime').empty();
            $('#txtconfName').empty();
            $('#txtconfName').val(data.name);
            $('#txtconfTime').val(date);
        },
于 2012-09-19T08:25:07.223 回答
1

例如,您可以:

public ActionResult PopulateConfirmationFields()
        {
            User usr = userProxy.GetUserById(UserAppContext.UserOID);
            string name = string.Format("{1},{0}", usr.FirstName, usr.LastName);
            String time = DateTime.Now.ToString();
            var t = new { name = name, time = time };
            return Json(t, JsonRequestBehavior.AllowGet);
        }
于 2012-09-19T08:15:18.277 回答