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

3
  • 1
    Could you elaborate "The first code works well if the value is a string, but for int (which have ID) is not working." Commented May 19, 2014 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. Commented May 19, 2014 at 21:17
  • it working ... nothing changed and it works ... yesterday did not work ... Thanks for your interest. Regards Commented May 20, 2014 at 13:53

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the quick reply. Unfortunately, BreakPoint in "public ActionResult People (int ID)" was not even called.
So the problem is the dataType of Controller , Integer vs String ?
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.
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 to check if it is id or code. And do likewise. Hope it helps .
Reasons unknown to me still null .... In any case, thanks for trying to help. Regards

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.