[英]Return Json from MVC controller but Date format not proper in javascript
我在一个项目中工作,我使用数据库中的数据创建网格,在我的控制器中我有这个代码
List<IEmployeeEntity> list = new Employee(connectionString).GetEmployeeRecord();
它返回我的员工列表,其中包含一些日期,然后我使用return Json(list)将其转换为Json; 但我在我的java脚本网格中得到的日期格式如/ Date(1325075075113)/我的javascript代码就像
$.ajax({
url: ../getRecord,
type: 'POST',
data: {},
async: false,
success: function (result) {
if (result !== "") {
Create Grid
}
}
});
我为这种情况创建了两种扩展方法
/// <summary>
/// Converts the value of the current System.DateTime object to its equivalent string representation using the specified format and culture-specific format information.
/// </summary>
/// <param name="date">DateTime instance</param>
/// <param name="format">A standard or custom date and time format string.</param>
/// <returns>A string representation of value of the current System.DateTime object as specified by format and provider.</returns>
public static string ToFormatString(this DateTime date, string format) {
return date.ToString(format, new CultureInfo("en-US"));
}
/// <summary>
/// Returns the number of milliseconds since Jan 1, 1970 (useful for converting C# dates to JS dates)
/// </summary>
/// <param name="dt">Date Time</param>
/// <returns>Returns the number of milliseconds since Jan 1, 1970 (useful for converting C# dates to JS dates)</returns>
public static double UnixTicks(this DateTime dt) {
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = dt.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
}
你可以选择其中任何一个。 要将日期转换为字符串,您可以简单地执行,
var dateString = myDate.ToFormatString("dd/MM/yyyy");
您不必担心机器的文化。
希望这对你有所帮助。
它返回服务器端日期格式。 您需要定义自己的函数来更改日期格式。
function jsonDateFormat(jsonServerDate) {
// Changed data format;
return (new Date(parseInt(jsonServerDate.substr(6)))).format("mm-dd-yyyy / h:MM tt");
};
它不是javascript问题我认为你需要根据需要在代码中形成日期,即仅在C#代码中。
像下面这样的东西可能对你有帮助..
List<IEmployeeEntity> list = new Employee(connectionString).GetEmployeeRecord();
list.All(x => { x.mydate = x.mydate.ToString("dd/MM/yyyy"); return true; })
要么
当你的属性是datetime类型时尝试这个解决方案,因为在第一个属性中,如果属性类型是datetime,它会给你一个错误
var q = from o in MyList
select new { mydate = x.mydate.ToString("dd/MM/yyyy"),
prop1 = o.prop1,
prop2 = o.prop2
};
是的,这是服务器端(代)问题。 什么是Employee实体中的日期属性的类型。 默认行为调用其ToString()方法。
我用这种方式完成了这样的事情:
将网格放入部分视图中。 从你的控制器返回json返回局部视图:
List<IEmployeeEntity> list = new Employee(connectionString).GetEmployeeRecord();
return PartialView("GridPartial", list);
在您的视图中:1。使用: @model IEnumerable.
2.添加包含部分的div:
<div id="partial">
@Html.Partial("GridPartial", @model)
</div>
然后在你的ajax:
$.ajax({
url: ../getRecord,
type: 'POST',
data: {},
async: false,
success: function (result) {
if (result.indexOf("<!DOCTYPE html>") == -1) {
$("#partial").empty();
$("#partial").html(result);
}
}
});
在部分视图foreach中的模型(yout list)并填充Grid ...
.Net使用的JavascriptSerializer生成特定的日期格式。
如果您希望将其格式化为客户端,可以使用以下内容将其转换为JavaScript日期:
var javascriptDate = new Date(parseInt(dateTimeInNetFormat.substr(6)))
我已经通过以下方式为自己解决了这个问题:
只需添加IEmployeeEntity 1额外字段 ,将根据需要格式化此DateTime,然后在回调时使用它。
class IEmployeeEntity
{
public DateTime StartDate {set; get;}
public DateTime FormatedStartDate { get { return StartDate.ToString("MM/dd/yyyy") } }
}
因此,只需在您的Javascript中使用FormatedStartDate ,您将获得正确的格式。
或者,如果你有一些View类,你就可以了
class IEmployeeEntity
{
private DateTime startDate;
public DateTime StartDate
{
set
{
startDate = value;
}
get {
return startDate.ToString("MM/dd/yyyy");
}
}
}
我通过以下方式解决了我的问题
List<IEmployeeEntity> list = new Employee(connectionString).GetEmployeeRecord();
return Json(
list.Select(
n => new {
n.key1,
AddedOn = n.AddedOn.Value.ToShortDateString() : String.Empty,
n.key2, n.key3
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.