简体   繁体   中英

C# DropDownList postback

I am using MVC ASP to create a series of dropdownlist's that are populated by SQL stored procedures. 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.

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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