Month: May 2014

KENDO UI TIP 4 : Getting cell values by Column number In Kendo Grid

Many a times we place a hidden column in the kendo grid and use it’s value for performing certain operations.

You can basically place this column at any position on the columnList and retrieve it by it’s index like below:

Untitled

Normally this logic works but if the grid is grouped by some columns then same number of empty columns with whom the grid is grouped will be inserted in the grid tbody at the very start.

Suppose the grid is grouped by two columns then two columns will be inserted at the starting.

In this scenario suppose we placed our hidden column at 1st place in the column list then practically it will be shifted to the first + (number of columns with which the grid is grouped) position.

so¬†row.find(‘td:nth-child(yourColumnIndex)’).text(); will give us a wrong value.

Solutions:


There are two possible options for avoiding this situation:

1> Place the hidden column at the last position and try to retrieve it’s value like below:

Untitled2

When we place the hidden column at the last position then it does not matter that how many columns are inserted at the starting as the last column will always be a last column.

2> Above solution is the best solution in most of the situations but what if we have to place more hidden columns.

In this case we will find out the number of columns with which the kendo grid is grouped and will add this number to the hidden column original position

Untitled2

Hope this helps. ūüôā

Implementing MongoDB With MVC

First of all apologies for being away from a long time.

In this post we will be setting up MongoDB in an MVC application. Before starting on it I expect that the audience has good knowledge of MongoDB.

MongoDb is a schema free, scalable, high performance document database. When we say schema less it blows mind of many SQL developers. Schema independent feature of MongoDB provides flexibility to the developer to develop the schema as they progress with the application.

Prerequisite : MongoDB should be up and running in the system. Follow the Mongo installation steps if you are a newbie.

Now we require to create a new MVC Application . Open Visual Studio and create a MVC project in it ,I have given MongoDB as the name of the project.

For integrating the MongoDB in MVC application we need to include two below mentioned DLL’s that we can do through NuGet packages.

1> MongoDB.Bson.dll (MongoDB Bson dll , MongoDB deals in documents and the format of documents is Bson )

2> MongoDB.Driver.dll (MongoDB C# Driver)

After setting up the required dll in the MVC project references. We will move forward to create a database in Mongo and reference it for all the operations.

We will create two settings in the project properties Connection String and DataBase name so that they can be accessed globally in the app. Below is a pictorial Demonstration.

Image

I have given names to these settings as MongoConnString (“mongodb://localhost” is the default value for Mongo Connection String) and MongoDBName.

Now we will create a Context class in App_Start Folder of MVC project. We can get the database in the individual controllers also but creating a context class is always a good practice in Web applications.

MongoDB Context Class :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Driver;
using MongoDB.Properties;

namespace MongoDB.App_Start
{
public class MongoContext
{
public MongoDatabase mgDatabase;
public MongoContext()
{

var client = new MongoClient(Settings.Default.MongoConnString);
var server = client.GetServer();
mgDatabase = server.GetDatabase(Settings.Default.MongoDBName);

}

}
}

So this is the context class , Now where ever we need to perform operations with the database we will create an instance of this context class.

For the operations we will be needing a Model to deal with. Let’s create a Model named as Rental with few basic properties:


using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MongoDB.Rentals
{
public class Rental
{
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Description { get; set; }
public int NoOfRooms { get; set; }
public string ImageID { get; set; }

[BsonRepresentation(BsonType.Double)]
public Decimal Price { get; set; }
public Rental()
{

}

public Rental(PostRentals postRentals)
{
Description = postRentals.Description;
NoOfRooms = postRentals.NoOfRooms;
Price = postRentals.Price;
Address = (postRentals.Address ?? String.Empty).Split(‘\n’).ToList();

}

}
}

To deal with these rentals stored in the database we will create a interface in the Mongo context class and use it for the rental operations.

public MongoCollection<Rental> RentalCollection
{
get
{
return mgDatabase.GetCollection<Rental>(“RentalCollection”);
}
}

Controller :


In the controller first we will create a instance of the Mongo context class and then will use RentalCollection interface for any type of operations on Rentals i.e Retrieval, Deletion ,Creation and Updation.

For querying the collection of documents we can use Query which is an extension method of MongoDB.Driver.Builder.

Query has a lot and lot of options for the different mathematical conditions like LTE(Less then and Equal to), GTE,EQ and many others.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MongoDB.Rentals;
using MongoDB.App_Start;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;

namespace MongoDB.Controllers
{
public class RentalsController : Controller
{

public readonly MongoContext context = new MongoContext();
//
// GET: /Rentals/

public ActionResult Index()
{
var rentals = context.RentalCollection.FindAll();
return View(rentals);
}

public ActionResult Post()
{
return View();
}

[HttpPost]
public ActionResult Post(PostRentals postRentals)
{
var rental = new Rental(postRentals);

//Inserting
context.RentalCollection.Insert(rental);

////Saving
//var objId = new ObjectId().ToString();
//rental.Id = objId;
//rental.Price = 40;
//context.RentalCollection.Save(rental);

//Updating
//var query = Query.EQ(“Description”, “jj”);
//var update = Update.Set(“Price”, “910”);
//context.RentalCollection.Update(query, update, UpdateFlags.Multi);
return RedirectToAction(“Index”);
}

public ActionResult Delete(string id)
{
var query = Query.EQ(“_id”, new ObjectId(id));
context.RentalCollection.Remove(query);
return RedirectToAction(“Index”);
}

}
}

So these actions defined in the controllers can be used to Show the Rentals, Update the Rentals and delete the Rentals.

We can also use LINQ with the Collections if we do not want to use the Inbuilt Query option.

So this is how we can set up MongoDB in MVC Application and perform the basic Database operations. Like everything else, there is a lot more to MongoDB then just this but this will get you started.