Posts

Showing posts from August, 2018

Donate

MongoDB Getting UTC Date Instead Of Local Date In ASP.NET MVC

Image
Hello all! When inserting local dates in MongoDB and retrieving them using C#, you might wonder why the date returned is inconsistent with the values stored in the database. Well, MongoDB treats those dates as UTC. So to display those dates as local, there are several solutions. Option one is to change the BsonElement attribute of a date property [BsonElement] [Required(ErrorMessage = "Activity Date required")] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime ActivityDate { get ; set ; } to BsonDateTimeOptions and set it's kind to local. [BsonDateTimeOptions(Kind = DateTimeKind.Local)] [Required(ErrorMessage = "Activity Date required")] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime ActivityDate { get ; set ; } Option two is to retain the BsonElement attribute of the date property. [BsonElement] [Required(ErrorMessage = "A...

Unable to get property call of undefined or null reference (jQuery Validation in ASP.NET MVC)

Hello, Our team encountered this issue before when using jQuery.Validation in ASP.NET MVC and the solution that we have is to replace the jQuery Validation Plugin from version 1.8.0 to 1.11.1 . Until now, were still using this version. Cheers!

Unable To Pass MongoDB's Object ID To Update Controller Action In ASP.NET MVC

When passing a model to the Update action, you may notice that the id field contains series of zeroes instead of the GUID value given the action below. public ActionResult EditActivity (UserActivity userActivity) { if (userActivity != null ) { userRepository.Update(userActivity); } return RedirectToAction ( "Index" ); } I already have set the @Html.HiddenFor() in the page with the value of the ID. After doing some research, I came up with the solution which is to change the @Html.HiddenFor(model => model.Id) element to @Html.Hidden(). @using (Html.BeginForm("EditActivity", "Home", FormMethod.Post)) { @Html.AntiForgeryToken() @Html.Hidden("id", Model.Id) //your html form elements here... } And in the controller action, add another parameter for the id which is of type string. public ActionResult EditActivity (UserActivity userActivity, string id) { if (userActivity != null ) { userActivity.Id = ObjectId.Pars...

Return Last Inserted ID In MongoDB Using C# In ASP.NET MVC

Good evening developers! The current project that I'm working with retrieves records from SQL Server and MongoDB. And it's my first time to use the NoSQL db in a real world project. I've attended training before but using the NoSQL db was not approved by the customer. Going back to the topic of this post, I was wondering if there's a similar approach in MongoDB to return the last inserted _id just like in C#'s code like this: int id = Convert.ToInt32(cmd.ExecuteScalar()); After experimenting and debugging, I can get the newly inserted record's id right after the insert statement by retrieving the id from the newly inserted BSONDocument itself. public void Add (UserActivity userActivity) { var activityDocument = new BsonDocument { { "ActivityUserName" , userActivity.ActivityUserName }, { "ActivityName" , userActivity.ActivityName }, { "ActivityModule" , userActivity.ActivityModule }, { "ActivityDate" , user...

Cannot read property 'DateSorter' of undefined in Bootstrap Table

Good evening gents! Doing some refactoring to one of my projects using the JavaScript prototype pattern, I encountered issues while calling the functions in prototype through the Bootstrap Table by Wenzhixin specifically in the data-sorter property. In document ready, I created an object of an EmployeeModel class that contains the function. $(document).ready( function () { EmployeeObj = new EmployeeModel(); }); And in the data-sorter property, I called the custom function to sort dates. <th data-field= "signout_time" data-sortable= "true" data-searchable= "true" data-sorter= "EmployeeObj.DateSorter" > @Html.DisplayNameFor(model => model.EmployeeList[0].SignOutTime) </th> Doing so, throws an exception "Cannot read property 'DateSorter' of undefined". After further investigation, the table has set values for data-sort-name and data-sort-order properties. <table id= "" data-sort-name=...

Custom JavaScript Date Sorter Function For Bootstrap Table in ASP.NET MVC

Image
While working on a project that implements Bootstrap Table by Wenzhixin, one column returns a date value with the format of MMM-YYYY and this column is sortable. A solution I've come up is to use the flexibility of Moment.js. Here's to date sorter function for the column. function DateSorter(a, b) { var dateA = new Date(moment(a).format( 'MMM-YYYY' )); var dateB = new Date(moment(b).format( 'MMM-YYYY' )); if (dateA < dateB) return -1; if (dateA > dateB) return 1; return -0; } Then set the data-sorter property of the table column with the function name. <th data-field= "BillingMonth" data-sortable= "true" data-sorter= "DateSorter" >Billing Month</th> Output Sample Fiddle: Custom JavaScript Date Sorter Function For Bootstrap Table Using Moment.JS

Passing Data From One Window To Another in WPF

Good afternoon everyone! When passing data from one window to another in WPF, the ideal approach is to set the both window's DataContext properties with the same ViewModel. In such a way, if a property in a View Model is updated in window1, window2 can access that property and the new value that has reflected. The snippet below show a child window and set's it's DataContext property using the main window's DataContext property. So whatever changes happen to a view model's property value, main window can also get a copy of that new value. public void ShowWindow(Window childWindow, bool isDialog) { //Application.Current.Windows[0] is the main window //set DataContext of child window with DataContext of main window var vm = Application.Current.Windows[0].DataContext; if (childWindow != null ) { childWindow.DataContext = vm; if (isDialog) childWindow.ShowDialog(); else childWindow.Show(); } }

Donate