Asp.net mvc 4 获取控制器mvc4中的下拉值和文本

Asp.net mvc 4 获取控制器mvc4中的下拉值和文本,asp.net-mvc-4,Asp.net Mvc 4,我正在从事MVC4项目。我有一个表单,其中dropdownlist由文本和值字段填充 @Html.DropDownList("SourceDropDownList", new SelectList(""), "-Select-", new { @class = "validate[required]" }) 此下拉列表由其他下拉列表更改事件填充 这是密码 function OnSourceFacilityDropDownChange(source, e) { $("#Sourc

我正在从事MVC4项目。我有一个表单,其中dropdownlist由文本和值字段填充

@Html.DropDownList("SourceDropDownList", new SelectList(""), "-Select-", new { @class = "validate[required]" })
此下拉列表由其他下拉列表更改事件填充 这是密码

function OnSourceFacilityDropDownChange(source, e) {
        $("#SourceDropDownList").empty();
        var curOpt = new Option('-Select-', "");
        $("#SourceDropDownList").get(0).options[$("#SourceDropDownList").get(0).options.length] = curOpt;

        if (source.value != '') {
            var url = getUrl() + '/AdminPanel/GetIns/?id=' + Math.random();
            $.ajax({
                url: url,
                data: { clientid: $("#SourceDropDown").val(), strFacility: source.value }, //parameters go here in object literal form
                type: 'GET',
                datatype: 'json',
                success: function (data) {
                    $.each(data, function (index, item) {
                        var curOpt = new Option(item.T, item.T);
                        curOpt.setAttribute("title", item.T);
                        $("#SourceDropDownList").get(0).options[$("#SourceDropDownList").get(0).options.length] = curOpt;
                    });
                },
                error: function (request, status, error) { alert("Status: " + status + "\n Exception Handling :  \n" + request.responseText); },
                complete: function () {
                    $("#divLoading").hide();
                }
            });
        }
        else {
        }
    }
AdminPanel/GetIns
控制器中的代码是

    public JsonResult GetInspection(int clientid, string strFacility)
            {

var objlist = (from d in Context.tbl_insp
                               orderby d.str_insp ascending
                               where d.clientid.Equals(ClientId))
                               select new { T= d.str_inspname, V= d.dte_start.Value.ToShortDateString()}).ToArray();                

                Array InspectionList = objlist;
                return Json(InspectionList, JsonRequestBehavior.AllowGet);
            }
在模型类中,我初始化了dropdown的属性

public string SourceDropDownList{ get; set; }
现在我只得到我在SourceDropDownList下拉列表中选择的文本值


如何获得值呢?

试试这个,举个例子

查看

  @Html.DropDownList("CustomerId", (SelectList)ViewBag.CustomerNameID, "--Select--")
    @Html.DropDownList("CustomerNameId", new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), "-- Select --")
<script type="text/javascript">
    $(document).ready(function () {
  $("#CustomerId").change(function () {

            var Id = $("#CustomerId").val();

            $.ajax({
                url: '@Url.Action("GetCustomerNameWithId", "Test")',
                type: "Post",
                data: { CustomerNameId: Id },
                success: function (listItems) {



                    var STSelectBox = jQuery('#CustomerNameId');
                    STSelectBox.empty();
                    if (listItems.length > 0) {
                        for (var i = 0; i < listItems.length; i++) {
                            if (i == 0) {
                                STSelectBox.append('<option value="' + i + '">--Select--</option>');
                            }
                            STSelectBox.append('<option value="' + listItems[i].Value + '">' + listItems[i].Text + '</option>');

                        }

                    }
                    else {
                        for (var i = 0; i < listItems.length; i++) {
                            STSelectBox.append('<option value="' + listItems[i].Value + '">' + listItems[i].Text + '</option>');

                        }
                    }
                }


            });

        });
});
</script>
 public JsonResult GetCustomerNameWithId(string CustomerNameId)
        {
            int _CustomerNameId = 0;
            int.TryParse(CustomerNameId, out _CustomerNameId);
            var listItems = GetCustomerNameId(_CustomerNameId).Select(s => new SelectListItem { Value = s.CID.ToString(), Text = s.CustomerName }).ToList<SelectListItem>();
            return Json(listItems, JsonRequestBehavior.AllowGet);
        }
public class CustomerModel
{
    public int CustomerId { get; set; }

        public int CustomerNameId { get; set; }
}