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?
-
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
3 Answers
Reset to default 3You 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条)