简体   繁体   English

C#DropDownList回发

[英]C# DropDownList postback

I am using MVC ASP to create a series of dropdownlist's that are populated by SQL stored procedures. 我正在使用MVC ASP创建由SQL存储过程填充的一系列dropdownlist。 However, each successive dropdownlist needs to be populated by passing the selection of the previous list as a parameter to the procedure call. 但是,需要通过将先前列表的选择作为参数传递给过程调用来填充每个连续的下拉列表。 How can I POST the selection of the list created using: 如何发布使用以下方法创建的列表的选择:

@Html.DropDownListFor(x => x.environmentName, new SelectList(Model.environmentName))

?

I was attempting to save it to a modelView and then pass the view to the controller again, but I feel this is a poor way to go about it. 我试图将其保存到modelView中,然后再次将视图传递给控制器​​,但是我觉得这样做很糟糕。

Here is a JQuery solution I wrote for 3 cascading drop-downs with ajax callbacks to the controller to fill the next list based on the previous choices. 这是我为3个级联下拉列表编写的JQuery解决方案,这些下拉列表带有到控制器的ajax回调以根据先前的选择填充下一个列表。 This might get you going in the right direction. 这可能会让您朝正确的方向前进。

Select: <select id="category" style="width: 150px">
        <option></option>
        @foreach (string cat in ViewBag.Categories)
        {
            <option>@cat</option>
        }
    </select><span id="errorforcategory" style="color: red"></span>
<select id="subcategory1" disabled="disabled" style="width: 150px"><option></option>       </select>
<select id="subcategory2" disabled="disabled" style="width: 150px"><option></option></select>


<script type="text/javascript">
$("#category").change(function () {
    $("#subcategory1").load('@Url.Action("GetSubCategory")' + "?category=" +     $("#category").val());
    $('#subcategory2').empty();
    $('#subcategory2').append($("<option></option>"));
    $('#subcategory2').attr('disabled', 'disabled');
}).ajaxStop(function () {
    if ($('#subcategory1 option').size() > 2) {
        $('#subcategory1').attr('disabled', '');
    } else {
        $('#subcategory1').attr('disabled', 'disabled');
    }

});

$("#subcategory1").change(function() {
    if ($("#subcategory1").val().trim()) {
        $("#subcategory2").load('@Url.Action("GetSubCategory")' + "?category=" + $("#category").val() + "&subcategory=" + $("#subcategory1").val());
    } else {
        $('#subcategory2').empty();
        $('#subcategory2').attr('disabled', 'disabled');
    }
}).ajaxStop(function() {
    if ($('#subcategory2 option').size() > 2) {
        $('#subcategory2').attr('disabled', '');
    } else {
        $('#subcategory2').attr('disabled', 'disabled');
    }
});

And then in your controller you can call your Stored Proc using whatever method you like then build out your result option text. 然后,在控制器中,您可以使用任何喜欢的方法调用Stored Proc,然后建立结果选项文本。

public string GetSubCategory(string category, string subcategory)
    {
        string returnval = "<option></option>";
        if (!string.IsNullOrEmpty(subcategory))
        {
            foreach (
                var cat in
                    db.Categories.Where(c => c.category1 == category && c.subcategory1 == subcategory)
                      .Select(c => c.subcategory2)
                      .Distinct())
            {
                if (!string.IsNullOrEmpty(cat.Trim()))
                    returnval += "<option>" + cat + "</option>";
            }
            return returnval;
        }

        return Enumerable.Aggregate(db.Categories.Where(c => c.category1 == category).Select(c => c.subcategory1).Distinct().Where(cat => !string.IsNullOrEmpty(cat.Trim())), returnval, (current, cat) => current + ("<option>" + cat + "</option>"));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM