【问题标题】:Binding a DropDownListFor to a variable list将 DropDownListFor 绑定到变量列表
【发布时间】:2014-06-17 13:36:16
【问题描述】:

这行得通

@Html.DropDownListFor(model => Model.Assessment.Interviewer, Model.SiteListing)

这不是

<div class="resident-relationship">@Html.DropDownListFor(m => Model.HouseholdMembersListing.ElementAt(i).Relationship, Model.ResidentTypeListing, new { @class = "t150"})</div>

如何正确使用 @HTML.DropdownListFor 绑定到 Listanything> 以便在 POST 到控制器时它具有值?我在一个家庭中有很多居民的页面上有这个。我还有其他绑定,例如姓名和年龄,它们运行良好。但在关系上,我需要有下拉列表。

【问题讨论】:

    标签: asp.net-mvc-3 model-view-controller html-helper html.dropdownlistfor


    【解决方案1】:
     var ddlStates = '#dropdownlistId'  
    
    $.ajax({
                    cache:false,
                    type: "GET",
                    url: "@(Url.Action("GetStatesByCountryId", "Country"))",
                    data: "countryId=" + selectedItem + "&addEmptyStateIfRequired=true",
                    success: function (data) {
                        ddlStates.html('');
                        $.each(data, function(id, option) {
                            if( option.name=="Maharashtra")
                            {
                            ddlStates.append($('<option></option>').val(76).html(option.name));
                            }
                        });
    
    
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to retrieve states.');
                        statesProgress.hide();
                    }  
                });
    

    【讨论】:

    • 你的答案应该包含对你的代码的解释和它如何解决问题的描述。
    猜你喜欢
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2021-05-17
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多