Reputation: 3940
OK, I'm totally new to .NET MVC3 and I've run into a problem.
I am developing a mobile application using Jquery mobile and I wish to send data from the mobile application to the web-page. On the server I have this:
[HttpPost]
[ValidateInput(true)]
public ActionResult Save(FormCollection actionValues) {
int age = Int32.Parse(actionValues["age"]);
string fn = actionValues["first_name"];
string ln = actionValues["last_name"];
CreateAndStorePersonModel(age,fn,ln); // Dummy method, not important
return new HttpStatusCodeResult(200); // Thanks to 3nigma for this
}
What I want is to be able to get the actionValues and store them in a model, then store this model into a database. For the sake of this example we'll assume I want to store a "Person" with attributes: "first_name, last_name, age". Also I might expand this model in the future.
From the mobile app i run the following code:
$.ajax({
type: "POST",
url: "http://external.url/Save",
dataType: "json",
traditional: true, // default serialization (do I even need this?)
data: {
"age": data_age,
"first_name": data_fn,
"last_name": data_ln,
},
success: function(d) { alert("Success: "+d},
}).error(function(data, errorTxt, jqXHR) {
alert('Error: '+errorTxt);
});;
I got the 500 internal error, but thanks to 3nigma this is no longer the case.
EDIT:
When testing from my webserver I get a http 302 "Found" when checking inspector, but the data does not get saved. When compiling to mobile phone the .error handler kicks inn with "parseerror", but the data gets saved. Any idea why?
Answer:
The 302 "Found" came because I returned a view (thanks to 3nigma) Should return this:
return new HttpStatusCodeResult(200);
Upvotes: 1
Views: 531
Reputation: 31043
500 is internal server error
public ActionResult Save(FormCollection actionValues) {
int age = long.Parse(actionValues["age"]);// there error seems to be here you are boxing long into int
string fn = actionValues["first_name"];
string ln = actionValues["last_name"];
CreateAndStorePersonModel(age,fn,ln);
}
try instead
int age = Int32.Parse(actionValues["age"].ToString());
answering the comments
you dont need to include anything just return Json from the ActionResult like
[HttpPost]
public ActionResult Save(FormCollection actionValues) {
//your code here
return Json(new {IsSuccess="success" });
}
and in the ajax success handler you can access it like
$.ajax({
type: "POST",
url: "http://external.url/Save",
dataType: "json",
traditional: true, // default serialization (do I even need this?)
data: {
"age": data_age,
"first_name": data_fn,
"last_name": data_ln,
},
success: function(data) {
alert("Success: "+data.IsSuccess}, //will alert Success: success
}).error(function(data, errorTxt, jqXHR) {
alert('Error: '+errorTxt);
});;
Upvotes: 3
Reputation: 595
it may be your json data syntax which is creating issue
data:
{
"age" : intValue,
"first_name" : stringValueinSingleQuotes,
"last_name" : stringValueinSingleQuotes
}
more over you can have success : Handler() attribute in the $.ajax call. Handler() method will be called if call is a success.
Upvotes: 0