AntiForgeryToken With Jquery Ajax Calls Using ViewModels?

I have read through here about getting the AntiForgeryToken working with Jquery ajax reuests and I basically have to make sure I include the token with my post/ajax call using something like

data: { 
        "__RequestVerificationToken":
        $("input[name=__RequestVerificationToken]").val() 
    },

However... I am using ViewModels and creating my view model object, assigning my values and then JSON.stringify'ing it and passing it as the data (As per below)

        // Ajax call here
    // Make a view model instance
    var ajaxEditPermissionViewModel = new Object();
    ajaxEditPermissionViewModel.HasPermission = isChecked;
    ajaxEditPermissionViewModel.Permission = permission;
    ajaxEditPermissionViewModel.Category = category;
    ajaxEditPermissionViewModel.MembershipRole = role;

    // Ajax call to post the view model to the controller
    var strung = JSON.stringify(ajaxEditPermissionViewModel);

    $.ajax({
        url: '/Admin/Permissions/UpdatePermission',
        type: 'POST',
        dataType: 'json',
        data: strung,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            ResetTableAfterAjaxCall();
            ShowSuccessNotification();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            ShowUserMessage("Error: " + xhr.status + " " + thrownError);
            ResetTableAfterAjaxCall();
        }
    });

I'm a bit confused how I would pass the token in using my current setup?? Any advice greatly appreciated.

Answers


Is there a specific reason you feel the need to stringify your result? We just do this..

    $.ajax({
        type: "POST",
        data: {
            permission: Permission, 
            // other fields
        }...

See this article for how to pass your token using this way of passing parameters:

http://weblogs.asp.net/dixin/archive/2010/05/22/anti-forgery-request-recipes-for-asp-net-mvc-and-ajax.aspx


Need Your Help

AVAssetExportSession outputfile

iphone ios video compression avassetexportsession

How should the AVAssetExportSession output file look like? I'm trying to compress a video from an ALAsset item and it doesn't work. I'm guessing the output file has something to do with it.

I have a list with over a million objects in it, trying to find the fastest way to search through it

c# dictionary hash hashset ilist

I have a list that stores well over a million objects within it. I need to look through the list and update the objects found through the below query as efficiently as possible.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.