How to send parameters from javascript to ActionResult and then setting it to a field of a class. Thank you
-
Are you trying to call an MVC action from javascript using AJAX? – Lazy Coder Apr 05 '16 at 17:39
-
is a javascript function that gets a parameter of type integer and wish to send to the controller (example: public ActionResult crud (int language)), when I do not receive my test parameter, please help. – Elizabeth Apr 05 '16 at 17:49
-
You're going to have to post some code for help, all I can do atm is speak on it abstractly...there are tons of how-to's regarding this though that can be found via Google: https://msdn.microsoft.com/en-us/library/dd381533(v=vs.100).aspx http://stackoverflow.com/questions/16186083/making-a-simple-ajax-call-to-controller-in-asp-net-mvc https://www.youtube.com/watch?v=7faB8kV43eg – Lazy Coder Apr 05 '16 at 17:53
2 Answers
Script
$('#btnDemo').click(function () {
var value = $('#txtName').val();
$.ajax({
type: 'POST',
url: '@Url.Action("ActionName","ControllerName")',
data: { 'value': value},
dataType: 'json',
success: function (data) {
//write handler for success event
},
error: function (xhr) {
alert(xhr.responseText);
}
});
});
Controller Code
[HttpPost]
Public ActionResult ActionName(string value)
{
//suppose you have student class then you can set name property like below
Student obj = new Student();
obj.Nmae = value;
//To Do your code
}

- 1,239
- 12
- 23
In this scenario, you'll likely want to return some value from your Controller Action that you can use within your client-side code once your operation has been completed.
One thing that is important to remember is that since you are working in a Web-based environment, it's unlikely that you will have objects that are persisting across multiple requests. The behavior that you are trying to implement would most commonly be done by posting your values from Javascript to your MVC Controller, then saving / updating some content in a database and then returning the result (either a boolean to indicate success or an actual value).
Server-Side Controller Action
It looks like you are passing multiple values (an array) to your Controller, in which case, you'll need to indicate that for your parameter by changing your int
to an int[]
.
For example purposes, we will just sum your values in the array and return it. You can perform whatever type of behavior you want within this Controller however (e.g. updating an entry in your database, a static field, etc.)
[HttpPost]
public int sum(int[] input)
{
return idioma.Sum();
}
Client-side Code
Since you are passing an array of values, you'll want to ensure that when you make your AJAX call that the traditional
attribute is set to true
to support this :
$.ajax({
type: 'POST',
url: '@Url.Action("sum","ControllerName")',
data: { 'input': idiomas },
dataType: 'json',
traditional: true,
success: function (data) {
// This should return your sum
alert(data);
},
error: function (xhr) {
alert(xhr.responseText);
}
});
Your code seems to vary as idiomas
is initially defined as an array, but later it is set to the value 2
. You'll need to decide explicitly what you are going to be passing to your Controller Action and what you want to actually do with these values.

- 74,820
- 37
- 200
- 327
-
-
Correct. It ultimately depends on what you want to do within your Controller Action. Are you updating a property or an entity? Are you performing a calculating and returning the value? Depending on what you are trying to accomplish will be the best way to figure out what return type is best to use. – Rion Williams Apr 05 '16 at 19:39
-