Posts

Showing posts with the label MongoDB

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 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

Donate