重新填充MVC DropDownList

时间:2017-09-17 00:13:25

标签: asp.net-mvc

在视图中

我们有一个简单的搜索文本框和按钮

<div class="form-group form-group-sm">
    <label class="col-lg-2 control-label" for="inputLoginSearch">Search for HOA</label>
    <div class="col-lg-10">
        <div class="input-group" style="max-width: 358px;">
            <input id="inputLoginSearch" runat="server" class="form-control" type="text" placeholder="Enter search phrase" style="width: 100%;" />
            <span class="input-group-btn">
                <button id="SearchButton" runat="server" class="btn btn-info" type="button"><span class="glyphicon glyphicon-search"></span> Search</button>
            </span>
        </div>
    </div>
</div>

...和DropDownList

<div class="form-group form-group-sm" id="HOAListDiv">
    @Using (Ajax.BeginForm(Nothing, "Home/SelectFromHOAList", Nothing, New AjaxOptions With {.HttpMethod = "POST"}, New With {.id = "CategoryForm"}))
        @<div class="panel panel-default">
            <div class="panel-heading">Please select the property</div>
            <div class="panel-body">
                <div class="form-group">
                    @Html.DropDownListFor(Function(x) x.Connection_ID, Model.HOAList, New With {.id = "inputHOAList", .class = "form-control"})
                </div>
            </div>
        </div>
    End Using
</div>

搜索按钮会触发此脚本

$('#SearchButton').click(function () {
    var vData = $('#inputLoginSearch').val();
    jQuery.ajax({
        url: '@Url.Action("LoadHOAList", "Home")',
        type: 'POST',
        data: { HOAName: $('#inputLoginSearch').val() },
        success: function (data, textStatus, jqXHR) {
        }
    });
});

控制器最初加载列表

Function Index() As ActionResult
    Dim vHOAModal As New HomeModel
    vHOAModal.HOAList = ReturnHOAList("xxxxx")
    Return View(vHOAModal)
End Function

...并且搜索按钮会触发此

Function LoadHOAList(HOAName As String) As ActionResult
    Try
        ReturnedRows = 0
        If HOAName.Length < 3 Then
            Return Modal(ModalValidation("You must enter at least three letters!"))
        Else
            ModelState.Clear()
            Dim vHOAModal As New HomeModel
            vHOAModal.HOAList = ReturnHOAList(HOAName)
            If ReturnedRows = 0 Then
                Return Modal(ModalValidation(HOAName & " returned zero results!"))
            Else
                Return View(vHOAModal)
            End If
        End If
    Catch ex As Exception
        EmailError(ex, 24, PageName)
        Return Modal(ModalError(ex.ToString))
    End Try
End Function


Private Function ReturnHOAList(SearchText) As List(Of SelectListItem)
    Try
        Dim vList As New List(Of SelectListItem)()
        vList.Add(New SelectListItem With {.Value = 0, .Text = "Select Property", .Selected = True})
        strSQL = "SELECT Connection_ID, HOA_Name FROM HOA_Connections WHERE Connection_ID != 4 AND HOA_Name LIKE '%" & SearchText & "%' ORDER BY HOA_Name ASC"
        Using DS As DataSet = ReturnDataSet_Data(strSQL, 0, True)
            For Each Row As DataRow In DS.Tables(0).Rows
                vList.Add(New SelectListItem With {.Value = Row("Connection_ID"), .Text = Row("HOA_Name")})
                ReturnedRows += 1
            Next
        End Using
        Return vList
    Catch ex As Exception
        EmailError(ex, 43, PageName)
        Return Nothing
    End Try
End Function

我尝试了很多方法,但DropDownList没有重新填充更改的数据。它肯定是从数据库返回并填充列表...

有什么想法吗?

谢谢

0 个答案:

没有答案