I want to pass selected index of dropdownlist in View to a different controller. I use java script for getting index of dropdaownlist. I got the Index.
But I want to pass that index to another Controller on click of my button .
How can I do that ?
here is my Java script snnipet
$('#btnsubmit').click(function () {
var deviceid = $('#ddldevice option:selected').val();
$.ajax({
type: 'GET',
data: { deviceid: deviceid },
url: '@Url.Action("ViewSensorTable","Home")',
success: function (result) {
}
});
My Button Code
<input type="submit" id="btnsubmit" value="Next" class="btn btn-primary btn-sm" />
And here is the Controller where i want the index value
public ActionResult ViewSensorTable(int id)
{
return View();
}
Edited
$('#btnsubmit').click(function () {
var deviceid = $('#ddldevice option:selected').val();
$.ajax({
type: 'GET',
data: { id: deviceid },
url: '@Url.Action("ViewSensorTable","Home")',
success: function (result) {
}
});
data: { id: deviceid }- the name of the parameter you send to the server must match the name of the parameter the server is expecting. Otherwise it cannot map it. Also you can simplify$('#ddldevice option:selected').val();to$('#ddldevice').val();- jQuery will get the value of the selected option automatically.deviceidis when you make your AJAX call.deviceidis null, and therefore you send an empty value to the server. Of course in your action method,idis not allowed to be null. Do some simple debugging in your JavaScript to check what is happening.public ActionResult ViewSensorTable(int id) { return View(); }-- you should not be returning a whole View here - this will return everything including your Layout page with full HTML tags, head, body etc. This is not helpful when responding to an AJAX request. You should either return a small amount of HTML (via a Partia lView) which can be inserted into your existing full page in the browser, or some JSON data which JavaScript can read and process. You can find examples and tutorials of both designs online very easily.