AsNoTracking() to force your data context to get everything from the database

You can specify Entity Framework to get everything back from the database instead of using what has been already loaded from the database.

A use case would be that you attach an entity from the web form and you save everything. You may return the view from the entity loaded but this one won’t get the full object if it has been attached previously.

        
public Workout Get(int id)
{
     return DatabaseContext
              .SetOwnable<Workout>()
              .Include(x => x.Sessions)
              .Include("Sessions.WorkoutSessionExercises")
              .Include("Sessions.WorkoutSessionExercises.Exercise")
              .AsNoTracking()
              .Single(c => c.Id == id);
}

With the addition of AsNoTracking, the information won’t be get from the local storage of the database context but from the database.

Binding complex user template values to your Model with Asp.Net MVC

If you have simple class, even if this one contain underlying classes, Asp.Net MVC takes care of creating all html input for you and takes care to rebound everything into objects once the form is submitted back. However, if you have a custom control (called template in Asp.Net MVC) which let you add sub objects with multiple properties to your main object, you will have to handle it manually.

First of all, in this type of scenario, you should use UIHint or Html Helper to generate the control itself.

public class MyObject 
{
    public int MyObjectId{get;set;}
	
    [UIHint("MyTemplate")]
    public MyInnerObject ParticipationsRepartitionActif { get; set; }
}

public class MyInnerObject
{
   public int Uid{get;set;}
   public MyItems Items{get;set;}
}

public class MyItems
{
    public int Id{get;set;}
    public DateTime Date1{get;set;}
    public DateTime Date2{get;set;}
    public string Comment{get;set;}
}

The UIHint will generate the correct html output for the user. Let say that this UIHint lets you select two dates and a comment. When you click a button, a row is added to a grid which let the user enter dates and a comment. The problem is how to send back these 3 information to the server in a way that the user could enter several lines in the same time.

The solution is to send back a collection of property in a format that Asp.Net MVC will be able to map back to your model class.

The first thing to do is to create an hidden field to your template with the name of the property that your model has (or view model in the case you are binding a view model to the view). This will bound the property to your template with an hidden field. This hidden field will be the transportation channel to send a collection of information that your grid contains.

The second thing is to populate this hidden field. You will need to set in your save button a Javascript that will loop through all lines and transform all html inputs into a serialized Json object.

$(document).on('click', '#mySaveButton', function (e) {
	//1) Transform here all grid lines into the hidden field
	
	//2) Be sure you do not prevent default action or return false because we want to server to post normally.
});

This Json object will be in a string format where we will insert it into the hidden field. Once saved, the hidden field is sent has other normal html field and Asp.Net MVC will deserialize it correctly.

 function SetHiddenField() {
    var obj = {};
    obj.Items = [];
    $('myControlThatHasAListOfItems').each(function () {
        var myObjectToSerialize = {};
        myObjectToSerialize.ID = $(this)...;
        myObjectToSerialize.Date1 = $(this)...;
        myObjectToSerialize.Date2 = $(this)...;
		myObjectToSerialize.Comment = $(this)...;
        obj.Items.push(myObjectToSerialize);
    });
    $('#MyHiddenField').val(JSON.stringify(obj));
}

Of course, do not create a Javascript function hard coded like this one. You should create it in a way where you could have multiple of this custom template control in the same page without conflicting or having to edit this code in the future.

In this example, the hidden field should be called “Items” and will be bound with the hidden field. Asp.Net MVC will deserialize the Json when it will be ready to bound Items. Since all properties’ name match the binding is done by magic.

From here, you can use on your controller the model and save everything.

Complex binding

In the case the object contains a type that Asp.Net MVC cannot handle for deserializing, you will have to create a model binder.

This is done by creating in the Global.Asax.cs an entry for a model binding:

ModelBinders.Binders.Add(typeof(YourComplexClass<bool>), new YourComplexClassModelBinder());

And the binder. The following code have to be modified to fulfill your need.

public class YourComplexClassModelBinder:IModelBinder
{
	public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
	{
		var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
		var modelState = new ModelState
							 {
								 Value = valueResult
							 };

		YourComplexClass<bool> actualValue = null;
		if (!string.IsNullOrWhiteSpace(valueResult.AttemptedValue))
		{
			var settings = new JsonSerializerSettings();
			settings.Converters.Add(new JSonComplexConverterForYourComplexClass());
			try
			{
				actualValue = JsonConvert.DeserializeObject<YourComplexClass<bool>>(valueResult.AttemptedValue, settings);
			}
			catch (FormatException e)
			{
				modelState.Errors.Add(e);
			}
		}
		bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
		return actualValue;
	}

	public class JSonComplexConverterForYourComplexClass : JsonConverter
	{
		public override bool CanConvert(Type objectType)
		{
			return (objectType == typeof(I_XYZ<bool>));
		}

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			return serializer.Deserialize<XYZ>(reader);
		}

		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			throw new NotImplementedException();
		}
	}
}

This is often the case of Interface that need to be deserialized into a concrete class. In the above example, I_XYZ was the interface but when it’s the time to deserialize, Asp.Net MVC doesn’t know to which type to deserialize. This is why, we provide a model binder for the class that contains the property that has I_XYZ interface and provide to Json.Net library how to read the Json for deserialization.

What is WcfTestClient and where is it?

When you are consuming a web service, either if you are creating it and you want to debug this one or just to do a quick use of a service, Visual Studio comes with an external tool that can help you invoke methods of service.

This tool is called WcfTestClient and can be executed from the Developer Console.

VisualStudio2012_VSCommandPrompt

Inside the console you doesn’t need to insert any parameter (but you could set the url of the service).

wcftestclient

From here, inside the menu you can add services with a complete url to the webservice. Once it’s added, you will get all methods that can be invoked.

wcftestclient2

That’s about it. From here you can invoke methods by setting parameters (if those aren’t complex object) and see results.

Using a FilteredDbSet with Entity Framework to have dynamic Filtering

If you are using an application that require to have your user to own data and not let other to see or edit it, you need to specify to each of your entity a user id to whom the entity belong. All my entities that belong to a user inherit from IUserOwnable. This interface force the entity to have a UserId.

public interface IUserOwnable
{
    int UserId { get; set; }
}

From here, when I want to get the list of entity of my user I just need to specify in the where clause the logged user id and Entity Framework get me all entities that belong to this one. The following example show you how to get all workout from the logged user.

public override IQueryable<Workout> GetAll()
{
    return DatabaseContext.Set<Workout>().Where(e=>e.UserId == logguedUserId);
}

The problem is that you need to set the UserId every time. This can lead to a problem of security if a developer forget to add the condition. It also a pain to repeat this code everywhere. This is why a better way to do it is to have a FilteredDbSet which will automatically add this condition. Here is the FilteredDbSet class.

public class FilteredDbSet<TEntity> : IDbSet<TEntity>, IOrderedQueryable<TEntity>, IListSource where TEntity : class
{
	private readonly DbSet<TEntity> _set;
	private readonly Action<TEntity> _initializeEntity;
	private readonly Expression<Func<TEntity, bool>> _filter;

	public FilteredDbSet(DbContext context, Expression<Func<TEntity, bool>> filter, Action<TEntity> initializeEntity)
		: this(context.Set<TEntity>(), filter, initializeEntity)
	{
	}

	public IQueryable<TEntity> Include(string path)
	{
		return _set.Include(path).Where(_filter).AsQueryable();
	}

	private FilteredDbSet(DbSet<TEntity> set, Expression<Func<TEntity, bool>> filter, Action<TEntity> initializeEntity)
	{
		_set = set;
		_filter = filter;
		_initializeEntity = initializeEntity;
	}

	public IQueryable<TEntity> Unfiltered()
	{
		return _set;
	}

	public TEntity Add(TEntity entity)
	{
		DoInitializeEntity(entity);
		return _set.Add(entity);
	}
	public void AddOrUpdate(TEntity entity)
	{
		DoInitializeEntity(entity);
		_set.AddOrUpdate(entity);
	}
	public TEntity Attach(TEntity entity)
	{
		DoInitializeEntity(entity);
		return _set.Attach(entity);
	}

	public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, TEntity
	{
		var entity = _set.Create<TDerivedEntity>();
		DoInitializeEntity(entity);
		return entity;
	}

	public TEntity Create()
	{
		var entity = _set.Create();
		DoInitializeEntity(entity);
		return entity;
	}

	public TEntity Find(params object[] keyValues)
	{
		var entity = _set.Find(keyValues);
		if (entity == null)
			return null;

		
		return entity;
	}

	public TEntity Remove(TEntity entity)
	{
		if (!_set.Local.Contains(entity))
		{
			_set.Attach(entity);
		}
		return _set.Remove(entity);
	}


	public ObservableCollection<TEntity> Local
	{
		get { return _set.Local; }
	}

	IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()
	{
		return _set.Where(_filter).GetEnumerator();
	}

	IEnumerator IEnumerable.GetEnumerator()
	{
		return _set.Where(_filter).GetEnumerator();
	}

	Type IQueryable.ElementType
	{
		get { return typeof(TEntity); }
	}

	Expression IQueryable.Expression
	{
		get
		{
			return _set.Where(_filter).Expression;
		}
	}

	IQueryProvider IQueryable.Provider
	{
		get
		{
			return _set.AsQueryable().Provider;
		}
	}

	bool IListSource.ContainsListCollection
	{
		get { return false; }
	}

	IList IListSource.GetList()
	{
		throw new InvalidOperationException();
	}

	void DoInitializeEntity(TEntity entity)
	{
		if (_initializeEntity != null)
			_initializeEntity(entity);
	}

	public DbSqlQuery<TEntity> SqlQuery(string sql, params object[] parameters)
	{
		return _set.SqlQuery(sql, parameters);
	}
}

From here, you just need to call the DbSet that is filtered instead of the default one.

public override IQueryable<Workout> GetAll()
{
    return DatabaseContext.SetOwnable<Workout>();
}

Of course, the DatabaseContext is your class that inherit from DbContext. The SetOwnable method will call the FilteredDbSet.

public IDbSet<TEntity> SetOwnable<TEntity>() where TEntity : class, IUserOwnable
{
    return new FilteredDbSet<TEntity>(this, entity => entity.UserId == CurrentUser.UserId, entity => entity.UserId = CurrentUser.UserId);
}

As you can see, we create a FilteredDbSet and assign the user Id with the CurrentUser which is the logged user.

What is ScvUtil.exe?

If you want to consume web services inside your .Net application, a tool exist to help you create the .Net code to interact with web services. This tool is a console application called “ScvUtil.exe“.

You can find in this blog post the information concerning the location of this tool.

This application generate two files. The first file is a .cs file that contains the code of the proxy and the code of the DTO classes (complex class used by the service). The second file is a .config file that contains the information about how to connect to the web services.

To generate those files, you can simply call the ScvUtil application with the path of the webservice (the WSDL path). This is an URL.

svcutil.exe http://localhost:1234/MyService.svc?wsdl

The problem is that doing it with only a single parameter will output those file to your C:\windows\system32\ which is not clean and can cause security problem. However, it’s possible to tell where to output those two files but adding 2 parameters to the svcutil.exe tools.

svcutil.exe http://localhost:1234/MyService.svc?wsdl /out:"c:\myPath\proxy.cs" /config:"c:\myPath\config.config"

Explicit destinations give you the leverage to output the CS file and the configuration file where you want.

Where is located ScvUtil.exe?

To use the ScvUtil.exe you can use the Visual Studio command prompt and type svcutil.exe. If you want to use it from a command prompt, you can go to the following path (in Microsoft Windows 7 operating system)

C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\

And you’ll find the program. The easiest way is to use the Visual Studio Command prompt since it’s already in the correct folder. To use this Visual Studio console, go to the Start Menu and go to the Visual Studio folder under the Tools sub directory.

VisualStudio2012_VSCommandPrompt

How to have a 404 error page for your Asp.Net MVC 3 or MVC 4 website

This is a question that Google doesn’t provide many answer. Some people suggest using Nuget package that handle 404 error, some has solution that worked with Asp.Net MVC 1.0 But, at the end, it’s just a 404 webpage and this is why we won’t use any Nuget package. You can use a Nuget extension to handle this but from what I am concerned, this is like using a nuclear bomb just to push a nail. Instead, let’s focus how what’s happening.

nuget_notfoundplugin

404 is a status code that say that we have not reach an existing page on the server. This is convenient to have this kind of page when the user enter a wrong url into its browser. The 404 page show and tell to the user that he is still on your webpage but have reach a wrong destination.

First of all, if the user goes to a webpage that doesn’t exist (a wrong controller name, or non existing action of your controller) you will get an error 500. The error 500 mean an internal error server. I suggest you handle 404 an 500 the same way in Asp.Net MVC because we never use real page since they are dynamically generated. Most of the time, user will reach a control using routing which is not a real page. In both case (http error 404 and 500), nothing is show from any of your webpage and in both case you want to alert the user of his error.

To handle the status code, I suggest that you check the response code from the Context at the end of the web request. This can be hooked in the Global.asax.cs file.

protected void Application_EndRequest()
{
	var statusCode = Context.Response.StatusCode;
	if (statusCode == 404 || statusCode == 500)
	{
		Response.Clear();
		var routingData = new RouteData();
		//rd.DataTokens["area"] = "AreaName"; // In case controller is in another area
		routingData.Values["controller"] = "Error";
		routingData.Values["action"] = "NotFound";
		IController c = new ErrorController();
		c.Execute(new RequestContext(new HttpContextWrapper(Context), routingData));
	}
}

As you can see, we clean all responses and start a brand new response by invoking the error controller that we create. This controller needs to have an action that I have called “NotFound” in the ErrorController. This action will display a view where you will be able to display a message to your visitor that will indicate that the page desired doesn’t exist.

public ActionResult NotFound()
{
	ActionResult result;

	object model = Request.Url.PathAndQuery;

	if (Request.IsAjaxRequest())
	{
		if (Request.ContentType == "application/json")
		{
			result = Json(new { Error = "Server cannot threat your request." });
		}
		else
		{
			result = PartialView(ERROR_VIEW, model);
		}
	}
	else
	{
		result = View(ERROR_VIEW, model);
	}
	return result;
}

This is a solution that return the Error404 view that you can create yourself with the whole master page if the request is not Ajax. Otherwise, it return a partial view which is still the Error404 but without the layout around it. Keep in mind that this solution expect that when you are using Ajax that you have specify the content type to json and also that you handle in your Javacript the read of the property Error. If this one is defined, you have to handle it.

 $.ajax({
            type: 'POST',
            url: 'WrongPage/NotFound',
            data: JSON.stringify(jsonRequest),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data, text)
            {
                //Success here
            },
            error: function (request, status, error)
            {
                if(request.status == ...)
                {
                    //Read the error message from the request
                }
            }
        });

This is how to handle easily 404/500 errors with Microsoft Asp.Net MVC framework. Of course, multiple other solution exist. I found this one efficient. As you can see, I haven’t set any logging functionality and this is because I use ELMAH (previously seen in this blog) that handle to log wrong date routing.

How to save multiple entities in inheritance in the same time with Entity Framework 5.0

Not every page should be CRUD. In the case of our enterprise example, a software that handle workout exercise, we had firstly created a CRUD application. So, the user had to create the workout in 1 page, then go into another one to create sessions and then another to associate ever exercises into session. In fact, for every sessions of exercise the user had to go into a page, associate it with an existing exercise and then go back to the list and do it again and again. So, if you create a new workout with 5 sessions (for every days of the week) you have to go to 7 pages then for every exercise go into a page. That mean that if we have 10 exercises per training that we had to go into 57 pages! With the proposed solution to handle all exercises into the same page we only have 1 page to go.

Here is the database to inform you concerning the schema before going with Entity Framework to save multiple objects in the same time.

DatabaseWorkout

I won’t display the whole code for creating a Workout from A to Z but concentrate on the goal of this article : saving multiple entities. The entity we want to save is the one called “Workout”, we want to save the sessions and the exercises withing the same page. We won’t create new exercise from this page, neither muscle and a group of muscle. But, we will use them.

The first step is to get from the form all information. I won’t enter in detail here because they are multiple ways to do it. Let say that our Model is fully loaded. When I say Model, it’s the Workout class loaded with WorkoutSessions and WorkoutSessionExercises. It doesn’t mean that those objects have all scalar properties loaded. For example, we may have only the exercises ID and not the name of this one. In fact, you shouldn’t have everything loaded because the form shouldn’t let you edit exercise name. So, we only pass from the form to the controller information concerning unique identifier (in our case Ids) and some other information like the Order of the exercise (see the class WorkoutSessionExercises).

From there, the saving process start. The controller should call the service layer which call the repository. The big works start right here. The first thing to do is to setup a transaction scope. The main reason is that we need to delete exercises that the user might have remove when in a second time we want to add new exercises. We should remove all non used exercises first and then add everything. Otherwise, it won’t be able to differentiate in the data context which one is removed, and which one is added. The use of transaction scope create a protection if something goes wrong in the later stage that nothing will be deleted. The transaction scope used in the same one that was used in the past with Ado.net, in fact, Entity Framework use behind Ado.Net and it won’t be a problem to use it. Watch out! you will need to have MSDTC service running because multiple connexion to the database will be done.

msdtc

public int Update(Workout entity)
{
	using (var scope = new TransactionScope())
	{
		// Remove
		
		// Insert
		scope.Complete();
	}
}

This is how to code should be. A transaction that is before and after remove instruction and insert intruction.

In our case, the code looks like this :

public int Update(Workout entity)
{
	int count = -1;
	using (base.DatabaseContext.RemoveValidation())
	{
		using (var scope = new TransactionScope())
		{
			RemoveWorkoutSessionExercisesNotUsedAnymore(entity);
			SetupWorkoutForRepository(entity);
			DatabaseContext.UpdateOwnable(entity);
			var ent = DatabaseContext.SetOwnable<Workout>().Local.SingleOrDefault(d => d.Id == entity.Id);
			DatabaseContext.Entry(ent).CurrentValues.SetValues(entity);
			count = DatabaseContext.SaveChanges();
			scope.Complete();
		}
	}
	return count;
 
}

Has you can see, few things are added. First, the DatabaseContext is using a USING with RemoveValidation method. This is a custom method that remove temporary all validations by Entity Framework. The code call ValidateOnSaveEnabled and set it to false and put it back to true once the scope of the using is over. This is ain’t a requirement for you, but I let it there for the reason that you may have to do it. The justification is quite simple. When you are saving from partial data that contain only the primary key (Id) and you attach these one you may have some error that some required field aren’t provided. If you tell Entity Framework not to check the validation, you won’t have this trouble.

Second, you see that we call RemoveWorkoutSessionExercisesNotUsedAnymore(…) followed by SetupWorkoutForRepository(…). The Remove function do a query to the database to get the list of existing entities and compare with the one provided by the form (from the web page). All entities that were there and that is not anymore are removed.

private void RemoveWorkoutSessionExercisesNotUsedAnymore(Workout entity)
{
	var listWorkoutSessionExerciseThatStillRemain = entity.Sessions.SelectMany(d=>d.WorkoutSessionExercises).Select(d=>d.Id).ToArray();
	var workoutSessionExerciseFromDatabase = DatabaseContext.SetOwnable<WorkoutSessionExercise>()
		.Where(d=>!listWorkoutSessionExerciseThatStillRemain.Contains(d.Id) &&  d.WorkoutSession.Workout.Id == entity.Id)
		;
	foreach (var sessionExercise in workoutSessionExerciseFromDatabase)
	{
	  DatabaseContext.SetOwnable<WorkoutSessionExercise>().Remove(sessionExercise);
	}
	DatabaseContext.SaveChanges();
}

The code is good for new and modified Workout entity. In the case of a new entity, the list of workout session exercise from the database will be empty and nothing will be removed. In the case of a modified Workout that contains workout session exercise, those are removed if they are not found in the list of Id that the user uses. At the end, we call SaveChange() which commit the delete statement to the Sql Server. Of course, since we are in a transaction, nothing will be really committed yet.

Third, the SetupWorkoutForRepository is called. This method is the heart of the work for modifying an entity with multiple children objects. Once the setup is done, the database is saved and the transaction is completed. At this final moment, every delete and insert are commited to the Sql Server Database.

Let’s take a look at the setup method which prepare the model classe to be saved by Entity Framework.

private void SetupWorkoutForRepository(Workout entity)
{
	if (entity.Sessions != null)
	{
		foreach (var workoutSession in entity.Sessions)
		{
			if (workoutSession.WorkoutSessionExercises != null)
			{
				foreach (var workoutSessionExercise in workoutSession.WorkoutSessionExercises)
				{
					if (workoutSessionExercise.Exercise != null)
					{
						if (workoutSessionExercise.Exercise.Id == BaseModel.NOT_INITIALIZED)
						{
							workoutSessionExercise.Exercise = null; //Should never go there
						}
						else
						{
							if (DatabaseContext.Set<Exercise>().Local.All(e => e.Id != workoutSessionExercise.Exercise.Id))
							{
								workoutSessionExercise.Exercise = DatabaseContext.Set<Exercise>().Attach(workoutSessionExercise.Exercise);
							}
							else
							{
								workoutSessionExercise.Exercise = DatabaseContext.Set<Exercise>().Local.Single(e => e.Id == workoutSessionExercise.Exercise.Id);    
							}
						}
					}

					if (workoutSessionExercise.Id == BaseModel.NOT_INITIALIZED)
					{
						//New workout session exercise
						DatabaseContext.SetOwnable<WorkoutSessionExercise>().Add(workoutSessionExercise);
					}
					else
					{
						if (DatabaseContext.Set<WorkoutSessionExercise>().Local.All(e => e.Id != workoutSessionExercise.Id))
						{
							DatabaseContext.SetOwnable<WorkoutSessionExercise>().Attach(workoutSessionExercise);
						}
					}
				}
				
				if (workoutSession.Id == BaseModel.NOT_INITIALIZED)
				{
					DatabaseContext.SetOwnable<WorkoutSession>().Add(workoutSession);
				}
				else
				{
					var dbEntry = DatabaseContext.SetOwnable<WorkoutSession>().Local.SingleOrDefault(e => e.Id == workoutSession.Id);
					if (dbEntry==null)
					{
						dbEntry = DatabaseContext.SetOwnable<WorkoutSession>().Attach(workoutSession);
					}
				}

				foreach (var workoutSessionExercise in workoutSession.WorkoutSessionExercises)
				{
					workoutSessionExercise.WorkoutSession = DatabaseContext.SetOwnable<WorkoutSession>().Local.SingleOrDefault(d=>d.Id == workoutSession.Id);
				}
			}
		}
	}
}

I’ll go through the whole method because almost all possibles care are treated. First, a Workout contains a list of session. Most of the time, this collection won’t be null and we will start looping through them. The same mechanism is executed for the WorkoutSessionExercises collection. Since, in our case, exercise aren’t deleted or modified directly into this screen, we have some case simplified. This is why we know that the exercise cannot be null. But, we know that the user can set an exercise to a session. Two scenarios occurs. The first one is that it’s the first time we use this exercise. If it’s the case, then we need to set Entity Framework an attached object that will represent the exercise. This is done with the line

workoutSessionExercise.Exercise = DatabaseContext.Set<Exercise>().Attach(workoutSessionExercise.Exercise);

The second one is then the exercise has already been used. So, it has already been attached or has been added. We use the Local property to get the instance of the entity from Entity Framework this give us the certitude that we are working on the same Exercise.

workoutSessionExercise.Exercise = DatabaseContext.Set<Exercise>().Local.Single(e => e.Id == workoutSessionExercise.Exercise.Id);    

At the end, we do the same for the workout session exercise. Why do we do exercise first and then we do exercise session? Because attaching or adding the exercise session would result to attach their sub property which is not what we want because we want to control if we attach or not the entity. We want to control what is attached and added and this is why we need to attach stuff from the bottom to up.

Also, you may want to save some property and not everything. In that case you can select the property and indicate if this one has changed or not.

DatabaseContext.Entry(dbEntry).Property(d => d.Name).IsModified = false;

This is an example that we could have set for the WorkoutSession if we would have like not to change the WorkoutSession name.

To conclude, it’s possible to save multiple objects that are composed withing a single Entity Framework data context. This approach is not without negative effect. You will soon realize that it requires some time to code every attachment correctly and depending of how your objects are composed and how you use Entity Framework (with or without ID for each foreign key, with or without loading the full graph) your configuration will change.

Convert Javascript object into Json string

When developing Javascript intensively at some point you may want to save the object. To save your Javascript object, you need to send it back to the server. Sometime, you may be able to send the object directly by using Javascript’s Ajax functionality and use the converted object with Asp.Net MVC, other time, you may want to do it manually, so sending the object into a string and to deserialize into C# class.

The first approach is the simplest for simple object that you have the same meta structure into your C# class and Javascript object. For example, if you have a c# class that looks like this:

public class MyClass
{
  int a;
  int b;
}

You would need to send from Javascript an object where Asp.Net MVC could map:

var xyz = {a=1,b=2};

You can use JQuery to send Ajax:

 $.ajax({
            url: 'MyController/MyAction',
            contentType: "application/json; charset=utf-8",
            type: "POST",
            data: JSON.stringify(xyz),
            dataType: "json",
            success: function (data) {
              //...
            },
            error: function () {
              //...
            }
        });

As you can see, we pass the data information using JSON.stringify() which send the object serialized into a string format. The contentType property indicates in which format the data is sent, meanwhile the property dataType is how to data received back (in the success, error, complete function).

When the function return the data, this one is in a string format. If the desired return is Json, like the example above by specifying that the dataType is “json”, we need to deserialize the string into Javascript object. JQuery provides the method jQuery.parseJSON(). This JQuery function calls the native ‘window.JSON.parse()‘, if the function doesn’t exist for the browser, it falls back to the eval function.

success: function (data) {
              var obj = jQuery.parseJSON(data);
            }