0

He wants to pass ID of the selected item from the DDL (items are retrieved from the database in the controller to ViewBag) from the list to the controller where it will handle itself.

The first code works well if the value is a string, but for int (which have ID) is not working.

View:

@using (Html.BeginForm("DDL", "Store", FormMethod.Post,
new { id = "Form1ID",
      data_PeoplAction = @Url.Action("People")
}))
{
<fieldset>
    <label for="Cities">Select a city</label>
    @Html.DropDownList("Cities", ViewBag.Cities as SelectList,
     new { id = "CitiesID" })
</fieldset>
}

<script src="@Url.Content("~/Scripts/populat.js")"></script>

If CityCode is such as "LA", "NY" value is passed correctly, and the controller change (int ID) for the (string Code) data are correctly

ViewBag.Cities = new SelectList(DB.myBase, "CityCode", "CityName");

what does not work with ID

Controler:

    public ActionResult DDL()
    {
        ViewBag.Cities = new SelectList(DB.myBase, "CityID", "CityName");
        return View();
    }

    public ActionResult People(int ID)
    {
        var peop = from s in DB.myBase
                           where s.CityID == CityID
                           select s.Population;

        ViewBag.Popul = peop;
        return View();
    }

populat.js:

$('#CitiesID').change(function () {
    var URL = $('#Form1ID').data('PeoplAction');
    $.getJSON(URL + '/' + $('#CitiesID').val(), function (data) {
    });
});

The second gets good ID but I can not pass to the controller.

$('#OneID').change(function () {
    var e = document.getElementById("OneID");
    var strUser = e.options[e.selectedIndex].value;
}

I'll be grateful for the help

user3128303
  • 747
  • 1
  • 11
  • 26
  • 1
    Could you elaborate "The first code works well if the value is a string, but for int (which have ID) is not working." – Runcorn May 19 '14 at 21:14
  • 1
    I'm struggling with your English. It's not clear where this is failing. That said, your second function only needs this: `var strUser = $(this).val();` instead of the two lines you have. – isherwood May 19 '14 at 21:17
  • it working ... nothing changed and it works ... yesterday did not work ... Thanks for your interest. Regards – user3128303 May 20 '14 at 13:53

1 Answers1

0

You could send the selected value as a parameter to the given URL using data attribute of getJSON instead of appending to the URL. Hope this solves your problem :

 $('#OneID').change(function () {
        var URL = $('#FormID').data('iLtAction');
        var selectedValue = $(this).val();
        $.getJSON(URL, { someValue : selectedValue }, function (data) {
        });
    });

And access someValue from Controller.

For more info getJSON

Runcorn
  • 5,144
  • 5
  • 34
  • 52
  • Thanks for the quick reply. Unfortunately, BreakPoint in "public ActionResult People (int ID)" was not even called. – user3128303 May 19 '14 at 22:00
  • So the problem is the dataType of Controller , Integer vs String ? – Runcorn May 19 '14 at 22:30
  • When I change to the string method is called but the value is null. When is an int, the method is not called. as instead of ID (int) in ViewBag will add such CodeCity (string) is also working correctly. – user3128303 May 19 '14 at 22:34
  • So what you could do is send the Integer value as "String" by using something like "num.toString();" to Controller and check if it is an Integer or again inside the controller you could use [int.TryParse](http://stackoverflow.com/questions/6137985/c-sharp-code-to-check-whether-a-string-is-numeric-or-not) to check if it is id or code. And do likewise. Hope it helps . – Runcorn May 19 '14 at 22:43
  • Reasons unknown to me still null .... In any case, thanks for trying to help. Regards – user3128303 May 19 '14 at 22:54