2

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) {


        }

    });
8
  • 2
    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. Commented Jun 29, 2018 at 8:57
  • @ADyson I do that , I got it but now one more error occur . The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ViewSensorTable(Int32)' in 'CalReport.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters Commented Jun 29, 2018 at 9:01
  • 1
    @NeelDarji Check what deviceid is when you make your AJAX call. Commented Jun 29, 2018 at 9:03
  • @NeelDarji I would guess in that case that deviceid is null, and therefore you send an empty value to the server. Of course in your action method, id is not allowed to be null. Do some simple debugging in your JavaScript to check what is happening. Commented Jun 29, 2018 at 9:05
  • 2
    One other small thing, unrelated but useful: 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. Commented Jun 29, 2018 at 9:51

2 Answers 2

1

You need to pass deviceid with the ajax request. Also the parameter name in Action Method should match with the parameter you are sending using ajax. Try the below code:

View

    <input type="submit" id="btnsubmit" value="Next" class="btn btn-primary btn-sm" />

    $('#btnsubmit').click(function () {

        var deviceid = $('#ddldevice option:selected').val();
        alert(deviceid);  //Comment it after testing

        $.ajax({
            type: 'GET',
            data: { id: deviceid },
            dataType: "json",
            url: '@Url.Action("ViewSensorTable","Home")',
            success: function (result) {
               //do something with the result
            }
        });
}

Controller

[HttpGet]
public ActionResult ViewSensorTable(int id)
 {
     return View();
 }
Sign up to request clarification or add additional context in comments.

19 Comments

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ViewSensorTable(Int32)' in 'CalReport.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
Is there any value in deviceid?
I dont know ,I am new to MVC.
@ADyson I added it for deep objects as a generalized solution while hit and trial. However, you made a good point about confusion if there is only single value. I'll remove it.
I'm asking for testing purpose to send the value as simply '1' to check whether the ajax call is working fine or not.
|
0

Try replacing your URL as

url: '@Url.Action("ViewSensorTable","Home")' + '/' + deviceid,

Or If your Action is [HTTPPost] then you need to change,

type: 'POST',

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.