0

I am very new to MVC, and am try to set up a series of cascading drop down lists using this example.

But I am stuck a little bit, because I don't know how to get the value from the second drop down and send it to controller when I press the appropriate button.

Here is my view:

    <script type="text/javascript">
    $('#list').change(function() {
        var selectedClient = $(this).val();
        if (selectedClient != null && selectedClient != '') {
            $.getJSON('@Url.Action("SelectC")', { clientID: selectedClient }, function (client) {
                var campaingSelect = $('#clist');
                campaingSelect.empty();
                $.each(client, function(index, clients) {
                    campaingSelect.append($('<option/>', {
                        value: clients.value,
                        text: clients.text
                    }));
                });
            });
        }
    });
</script>
@using (Html.BeginForm("CreateNewCampaign", "Home", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    @Html.LabelFor(m => m.alreadyCreatedCampaignName, "Name:")
    @Html.DropDownList("clist","-- Select Client -2-")
    <input type="Submit" name="button" value="Select" class="btn btn-primary" />
}

Controller:

  public ActionResult SelectC(int clientId, CampaignModel model, FormCollection form)
        {
            Session["ClientID"] = clientId;

            ViewBag.ClientName = "You're using: " + Session["ClientName"];

            var CampaignInf = CampaignManagementService.GetCampaigns((string) Session["ticket"], clientId);
            List<AlreadyCreatedCampaignList> itemas = new List<AlreadyCreatedCampaignList>();
            foreach (var element in CampaignInf)
            {
                itemas.Add(new AlreadyCreatedCampaignList() {CampId = element.Key, CampName = element.Value});
            }

            var listOfCam = new SelectList(itemas, "campID", "campName", 1);
            return Json(listOfCam.Select(x => new {value = x.Value, text = x.Text}), JsonRequestBehavior.AllowGet);
        }

I want to get the value to other controller, and I am not sure of the right way to go about doing this.

Community
  • 1
  • 1
piotr
  • 45
  • 1
  • 7
  • The question was not so clear, Where is the second dropdown button which you want to filter based on the First dropdown. – Sravan Aug 25 '13 at 15:38
  • Button is here @Html.DropDownList("clist","-- Select Client -2-") With filtering everything is ok, but how to get value from other drop-down and transfer it to the controller – piotr Aug 26 '13 at 08:40

1 Answers1

0

You can get value of dropdownlist just by giving it ID and call $("#id").val();, then you can transfer it to the controller through ajax maybe.

Here is mine, try it
public ActionResult ActionName(string dropdown_value){ //your code }

<script>
$(document).ready(function(){
$("submit").click(function(){
$.ajax({
   url:"Controller/ActionName",
   datatype: "POST",
   data: { dropdown_value : $("#clist").val() },
   success : function(){ //your code if success }
});
});
});
</script>
Vinh
  • 73
  • 6