how to get data in Asp.net mvc using http post

say, I have controller method and a corresponding view (see below).

once I load the view, I have a self invoking JavaScript function, that calls the same controller method.
code below, shows the get request, when I test this, I get the value of 1 in the id parameter of my ProcessData method.
in Get request, the data is passed in the query string, which is mapped to my parameter name by ASP.NET ( I guess by the process of model binding). I understand this part.

My question is, if i wanted to do a post request and send a XML data over to the same controller method, how can I achieve this.

In a post request, how do we read data in the controller?

controller -
public ActionResult ProcessData(string id) {          
      return View();
}

view -

@{
    ViewBag.Title = "test";
}

<h2>test</h2>

@section scripts {
    <script type="text/javascript">
        (function () {
            $.ajax({
                type: "Get",
                url: '@Url.Action( "ProcessData", "Test")',
                contentType: "application/xml; charset=utf-8",
                data: {id : 1},              
                success: successFunc,
                error: errorFunc
            });
            function successFunc(data, status) {
                alert('success');
            }
            function errorFunc() {
                alert('error');
            }
    })();
    </script>
}
novice programmerAsked:
Who is Participating?
 
louisfrCommented:
You can pass it in the same way you pass "id".
1
 
louisfrCommented:
Is the xml in a file?
0
 
novice programmerAuthor Commented:
@louisfr - it is a XML string.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
novice programmerAuthor Commented:
@louisfr : I tried that and it did work, but not exactly how I expected. In the ajax call, i'm passing it as

 data: {xmldata : xmlString },     and in my controller method , I had to read the inputstream, because the string parameter xmldata was coming as "null".
any thoughts on this?

pubic void ReadRequest(string xmldata){
    var data = this.Request.InputStream;
}
0
 
louisfrCommented:
It may have to do with the size of the data.
I'm going to do some tests to check that.
1
 
louisfrCommented:
A ValueProviderFactory may be what you're looking for.
I found an example at http://www.nogginbox.co.uk/Media/files/XmlValueProviderFactory.txt
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.