mikha
asked on
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>
}
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>
}
Is the xml in a file?
ASKER
@louisfr - it is a XML string.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@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;
}
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;
}
It may have to do with the size of the data.
I'm going to do some tests to check that.
I'm going to do some tests to check that.
A ValueProviderFactory may be what you're looking for.
I found an example at http://www.nogginbox.co.uk/Media/files/XmlValueProviderFactory.txt
I found an example at http://www.nogginbox.co.uk/Media/files/XmlValueProviderFactory.txt