I have a model with many datetime properties, in this i have LastUpdatedTime property which i am using for Concurrency functionality and will not be binding to any control in view. and this datetime properties rendering in client side as /Date(1224043200000)/, while saving in mvc controller, it is not recognizing /Date(1224043200000)/ as valid format and taking default date and failing the save operation. so any common solution after fetching the model and before rendering in view, so that i can change the format for all datetime properties in the model.
Assuming that the date value you have in your controller as var /Date(1224043200000)/
Use the below code
HTML
<body ng-controller="MainCtrl">
<p>Hello ASP DATE ISSUE </p>
{{dateValue.slice(6,19) | date}}
</body>
JS
var app = angular.module('aspDateApp', []);
app.controller('MainCtrl', funscope) {
$scope.name = 'ASP DATE ISSUE';
$scope.dateValue='/Date(1224043200000)/';
});
LIVE DEMO
You should extract the milliseconds part from "/Date(1224043200000)/" (1224043200000) and Convert it into valid date format and bind it into the relevant attribute of your model.
var datetimeInMilliseconds = parseInt("/Date(1224043200000)/".match(/\(([^)]+)\)/)[1]);
var convertedDateTime = new Date(datetimeInMilliseconds);
ArrayName.AttributeName = convertedDateTime ;
Related
I have an ASP.NET MVC app. This app uses Razor as the view engine. I need to display a date/time to the user. My challenge is, the DateTime property on the model is in UTC time. I want to display the time as the user's local time. Currently, I have the following:
<div>
#if (Model.LastModifiedOnUTC.HasValue) {
Html.Raw(Model.LastModifiedOnUTC.ToString());
}
else {
<span>This record has not been updated</span>
}
</div>
I want to display the DateTime as something like "Wednesday, February 11, 2015 at 3:27 PM". However, I'm not sure how to do this. When I do the above, an empty string is printed. Which doesn't make any sense to me.
Thank you for any insights
One option is to use JavaScript.
in your View
<time>#Model.LastModifiedOnUTC.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)<time>
JS (my example with JQuery)
$("time").each(function (elem) {
var utctimeval = $(this).html();
var date = new Date(utctimeval);
$(this).html(date.toLocaleString());
})
You can use use following code in razor
<div>
#if (Model.LastModifiedOnUTC.HasValue) {
Html.Raw(Model.LastModifiedOnUTC.ToLocalTime().ToString());
}
else {
<span>This record has not been updated</span>
}
</div>
Can we access Umbraco Dictionary item value from JavaScript ?
As I am using Umbraco 6.0.5
No, you can't do it out-of-the-box.
I had to do this multiple times, and the best way I came up with, is to print all dictionary items as an object when the page loads (in <head> or something). Of curse only items for the current language.
So in your source you have
<script>
var dic = {"quantity":"Quantity","totalPrice":"Total price","securePayment":"Secure payment"};
</script>
And then get is as
window.dic["quantity"]
As far as I know, you can't do it out of the box - haven't actually tried, though.
I would first create a simple rest service in my website that returns the Umbraco dictionary item using a querystring parameter as the alias value
var alias = HttpContext.Current.Request.QueryString["alias"]
if(alias != null)
{
var dictionaryItem = umbraco.GetDictionaryItem(alias)
...
}
Then call your own webservice through javascript to get the value
What I have done is declare variable in using Javascript
<script type="text/javascript">
var UmbracoDicKeyValue = '<%= kraftvaerk.umbraco.Translations.translate("Umbraco_Dic_Key", lang) %>';
</script>
And for setting value of lang variable from server side.
protected string lang = (!String.IsNullOrEmpty(umbraco.library.Session("lang")) ? umbraco.library.Session("lang") : "en-GB");
umbraco.library.setSession("lang", lang);
Now use variable UmbracoDicKeyValue in javascript code.
I am 3 months into learning KnockoutJS and it has been great so far. However, I am facing an issue with binding.
This is the scenario:
I am using MVC with KO.
MVC model is passed down to the view, converted into a knockout object and pushed into the viewModel variable:
var data = ko.mapping.fromJS(#Html.Raw(Json.Encode(Model)));
var viewModel = new HP.ViewModels.CertificationPathViewModel(data);
ko.applyBindings(viewModel);
Within viewModel, I reference the MVC model as self.data:
ViewModels.CertificationPathViewModel = (function (data) {
var self = ViewModels.BaseEntityViewModel.apply(this, [data]);
// some other code
return { Data: self.Data, };
}
ViewModels.BaseEntityViewModel = (function (data) {
var self = this;
self.data = ko.observable(data);
// other code
return { Data: self.data, };
}
On the view, I data-bind like this:
<div id="drpControl" data-bind="CustomDropdown: Data().BusinessUnits.SelectedGroup, optionSettings: { CustomOptions: Data().Units.Groups, CustomOptionsCaption: '-- Select Group --' }"></div>
I try to update the self.data after an ajax call. I return the entire MVC model object and attempt to replace self.data like this :
self.data(updatedModel)
My expectation is that KO will take care of the update and no extra binding is needed. It works great for simple binding (ex. Value: Data().Something) but it doesn't work for complex binding (ex. value: Data().BusinessUnits.SelectedGroup ).
The controls that have complex binding are still bound to the old model, so KO doesn't know what to pass back next time I submit an ajax request.
Is this a limitation of KO, or I am not doing something properly?
Thanks
the ko.mapping plugin changes every property on self.data into an observable. During your update, you need to remap the updated data.
Since you didn't actually post your code, just unformatted snippets I can't help a whole bunch, but you should start by changing this line: self.data(updatedModel) to this:
ko.mapping.fromJS(updatedModel, self.data);
see the Knockout.JS mapping documentation
Protip for stack overflow - include your full code, to the extent that it's possible. Also, if you can, make a jsfiddle that reproduces your problem.
I got a currency exchange script from coinmill. I want to modify it to work nice in my page. I am a newbie in html/javascript programming thats why i am asking help on this one, pls.
<script src="http://coinmill.com/frame.js"></script>
<script>
var currency_round=true;
var currency_decimalSeparator='.';
var currency_thousandsSeparator=',';
var currency_thousandsSeparatorMin=3;
</script>
$199.00 (US) = <script>currency_show_conversion(199.00,"USD","GBP");</script> GBP<br>
<small>Currency data courtesy coinmill.com</small>
This scripts works fine but shows the conversion for the default value in the script. I need to replace the value ($199.00) to a value from a textbox of id "edit_1". Automatically after the user inserts the currency to exchange, the value will show in the page.
Thanks in Advance.
I am Stephen Ostermiller, and I run http://coinmill.com/. You can make this work with the JavaScript currency API from Coinmill:
Put on onchange event on the textarea
Get the value from the text area using this.value
use the currency_convert(value,from,to) method that is available in frame.js to convert it into the currency of your choice
Write the value to where you want it in the page, for example with document.getElementById('results').innerHTML
Putting it all together:
<script src="http://coinmill.com/frame.js"></script>
<script>
var currency_round=true;
var currency_decimalSeparator='.';
var currency_thousandsSeparator=',';
var currency_thousandsSeparatorMin=3;
</script>
<textarea id=edit_1 onchange="document.getElementById('results').innerHTML=currency_convert(this.value,'USD','GBP')"></textarea><br>
Converted into GBP:<div id=results></div>
<p><small>Currency data courtesy coinmill.com</small></p>
Whene I enter "273" into the textarea, I then see "Converted into GBP: 176.32" on the page.
You can test out a live example on jsfiddle: http://jsfiddle.net/wAxnq/
I have just started using the
<% Html.DevExpress().DateEdit()
control and i got it to work fine in my ASP.Net MVC application. The code is as shown below:
aspx page:
<% Html.DevExpress().DateEdit(settings =>
{
settings.Name = "EndDate";
settings.Properties.NullText = "dd/MM/yyyy";
settings.Properties.EditFormat = EditFormat.Custom;
settings.Properties.EditFormatString = "dd/MM/yyyy";
settings.Properties.DisplayFormatString = "dd/MM/yyyy";
settings.Date = Model.EndDate;
settings.Width = 100;
}
).Render();
%>
Above this code i have a reference to my javascript file (DateChanges.js) in this file i want to be able to do something like:
$(document).ready(function(){
$("#EndDate").change(function(){
//do whatever i want
});
})
I cant do this now cause using firefox i can see that the actual textbox that this datepicker assigns a value to has be named "EndDate_I". So my question is how can i easily do this since i want to be able to catch the change event of this control and play around with it in jQuery??
The DevExpress MVC Extensions offer their own infrastructure for the client-side processing needs (see the http://help.devexpress.com/#AspNet/CustomDocument6908 help topic to getting started).
It is necessary to handle the client-side ASPxClientDateEdit.DateChanged event, and retrieve the newly selected Date via the client-side ASPxClientDateEdit.GetDate() method. Use the retrieved js Date object for your additional needs:
<script type="text/javascript">
function OnDateChanged(s, e) {
var newDate = s.GetDate();
alert(newDate);
}
</script>
settings.Properties.ClientSideEvents.DateChanged = "OnDateChanged";
There is a rather long Blog post at http://kennytordeur.blogspot.com/2011/05/aspnet-mvc-where-is-clientid_10.html discussing your problem
( I think it is to long to have it pasted here, and the author deserves the credits )
following on from your comment on Mikhails's answer, there will be a property in the global namespace with the name of your control, so it's just like this:
CalculateDayDifference(s.GetDate(), EndDate.GetDate());
All the mvc controls do this, for some you might have to set the EnableClientSideApi property to start using them.