jquery - asp.net mvc 3 access DateTime model property in javascript - Stack Overflow

In Asp.Net MVC I have a model containing a property of type DateTime.How can I access it in JavaScrip

In Asp.Net MVC I have a model containing a property of type DateTime.

How can I access it in JavaScript?

Using @(Model.PrimitiveTypeProperty) works just fine, but not when it es to a property of DateTime.

The problem is more plex, but I need to at least have an alter with this value.
Is it possible?

In Asp.Net MVC I have a model containing a property of type DateTime.

How can I access it in JavaScript?

Using @(Model.PrimitiveTypeProperty) works just fine, but not when it es to a property of DateTime.

The problem is more plex, but I need to at least have an alter with this value.
Is it possible?

Share Improve this question edited Oct 3, 2012 at 11:01 tereško 58.5k25 gold badges100 silver badges150 bronze badges asked Oct 3, 2012 at 10:46 Sandra S.Sandra S. 1811 gold badge4 silver badges22 bronze badges 1
  • 1 Are you just looking to display the date/time using JavaScript or do you need it as a javascript Date object? – James Commented Oct 3, 2012 at 10:50
Add a ment  | 

3 Answers 3

Reset to default 3

You can put it in a hidden for example:

 @Html.HiddenFor(model => model.YourDate)

And then access it from javascript. After you modify it's value, on submit, you should get the value updated back to your controller.

And don't forget to put in in a form like:

@using (Html.BeginForm())

From your model you should have the DateTime property.

public class YourModel {

   public DateTime YourDateField { get; set; }

}

Then in yourView you could do:

@Html.LabelFor(m => m.YourDateField);

Accessing it using JavaScript/jQuery:

$(document).ready(function() {
   var d = $("#YourDateField").val(); 
   alert(d);

  $("#YourDateField").val = "01/01/2000"; // sets the date which will then be posted back to the model on the form submit. 
});

This will alert the date value from your model.

 $("#YourDateField").val = "01/01/2000"; 

Changes the date client side. If yor View has a Form element, when it is submitted back to the server the value passed back will reflect the change (01/01/2000).

If you are looking to basically translate your DateTime property to a JS Date, you could try something like:

var jsDate = Date.parse('@Model.EndDate.ToUniversalTime().ToString("r")');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745086905a4610460.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信