bdrelling
bdrelling

Reputation: 840

How do I send an XML document from jQuery (JS) to C# in an MVC project?

In my project, I load in an xml document from my server. I make one call to this using ajax and set it to a variable (called siteData).

I make manipulations to siteData actively on the page and allow users to change whatever they want to. When these operations are done, I need to save the XML document to the server.

I'm using C# in an MVC project server-side. I need for this method to receive the XML file I have made in JavaScript in order to parse it.

I figure I have two options:

  1. Figure out how the siteData variable is being received through C# and set that as the parameter instead of String in my C# method.

  2. Convert siteData into a string and send it to my C# method to parse.

I can't figure out how to make either option work. I just want to pass the manipulate XML file into my C# method to save it on the server.

How can I accomplish this?

(As a note, I cannot use any plugins or alternate libraries. I am using jQuery 1.7.2 and C#.NET.)

Upvotes: 1

Views: 2966

Answers (2)

Hassan Boutougha
Hassan Boutougha

Reputation: 3919

you can use webclient

http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

here a sample

 Console.Write("\nPlease enter the URI to post data to : ");
            string uriString = Console.ReadLine();
            // Create a new WebClient instance.
            WebClient myWebClient = new WebClient();
            Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
            string postData = Console.ReadLine();
            // Apply ASCII Encoding to obtain the string as a byte array. 
            byte[] postArray = Encoding.ASCII.GetBytes(postData);
            Console.WriteLine("Uploading to {0} ...",  uriString);                          
         myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

            //UploadData implicitly sets HTTP POST as the request method. 
            byte[] responseArray = myWebClient.UploadData(uriString,postArray);

            // Decode and display the response.
            Console.WriteLine("\nResponse received was :{0}", Encoding.ASCII.GetString(responseArray));

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039110

You could write a custom model binder. Let's take an example. Suppose you have the following controller:

public class HomeController : Controller
{
    // Serve the view initially
    public ActionResult Index()
    {
        return View();
    }

    // This will be called using AJAX and return an XML document to the
    // client that will be manipulated using javascript
    public ActionResult GetXml()
    {
        return Content("<foo><bar id=\"1\">the bar</bar></foo>", "text/xml");
    }

    // This will be called using AJAX and passed the new XML to persist
    [HttpPost]
    public ActionResult Save(XDocument xml)
    {
        // TODO: save the XML or something
        return Json(new { success = true });
    }
}

On the client we could have the following javascript:

<script type="text/javascript">

    // send an AJAX request to retrieve the XML initially
    $.ajax({
        url: '@Url.Action("getxml")',
        type: 'GET',
        cache: false,
        success: function (data) {
            // The data variable will contain the initial xml
            // Now let's manipulate it:
            $(data).find('bar').attr('id', '7');

            var xmlString = data.xml ? data.xml : (new XMLSerializer()).serializeToString(data);

            // Let's send the modified XML back to the server using AJAX:
            $.ajax({
                url: '@Url.Action("save")',
                type: 'POST',
                contentType: 'text/xml',
                data: xmlString,
                success: function (result) {
                    // ...
                }
            });
        }
    });
</script>

and the last part is to write a custom model binder for the XDocument type so that the Save controller action could get the XDocument:

public class XDocumentModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var request = controllerContext.HttpContext.Request;
        if (!request.ContentType.StartsWith("text/xml", StringComparison.OrdinalIgnoreCase))
        {
            return null;
        }
        return XDocument.Load(request.InputStream);
    }
}

that will be registered in Application_Start and associated to the XDocument type:

ModelBinders.Binders.Add(typeof(XDocument), new XDocumentModelBinder());

Upvotes: 1

Related Questions