获取控制器 mvc4 剃刀中的下拉值和文本



我正在做MVC4项目。我有下拉列表填充有文本和值字段的表单。

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

在模型类中,我已经初始化了下拉列表的属性

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

最新更新