0

我对 MVC 很陌生,我尝试使用此示例设置一系列级联下拉列表。

但是我有点卡住了,因为当我按下相应的按钮时,我不知道如何从第二个下拉列表中获取值并将其发送到控制器。

这是我的看法:

    <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" />
}

控制器:

  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);
        }

我想获得其他控制器的价值,但我不确定这样做的正确方法。

4

1 回答 1

0

你可以通过给它 ID 和 call 来获取 dropdownlist 的值$("#id").val();,然后你可以通过 ajax 将它传输到控制器。

这是我的,试试看
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>
于 2013-11-07T02:20:13.453 回答