如何将列表作为参数传递给MVC C#中的控制器

问题描述 投票:1回答:1

我有一个绑定到我的视图的列表,并且我还有一个URL Action重定向,我想将其纳入该列表并将其作为参数传递给我的控制器。我可以看到在调用URL操作时列表不为空,但是当它命中控制器时,列表的值为空

我的视图

 <table class="table table-bordered">
        <thead>
            <tr>
                <th>SearchTerm</th>
                <th>SelectedSearchCondition</th>
                <th>SelectedFieldToSearch</th>

            </tr>
        </thead>
        <tbody>
            @foreach (var item in ListOfConditions)
            {
                <tr>

                    <td>@item.SearchTerm</td>
                    <td>@item.SelectedSearchCondition</td>
                    <td>@item.SelectedFieldToSearch</td>

                </tr>
            }
        </tbody>


    </table>
}
    <script type="text/javascript">
        $(document).ready(function () {
            $('#loadSearchResults').on('click', loadFilteredSearchResults)
        });
        function loadFilteredSearchResults() {
            //alert('I have been clicked')
             window.location.href = '@Url.Action("DisplayFilteredSearchResults", "FilteredSearch",ListOfConditions)';
        }
    </script>

**我的控制器方法**

public ActionResult DisplayFilteredSearchResults(List<string>ListOfConditions)
        {

            ListOfConditions.Count();
            //ListOfConditions.Add(searchInfo);
            //var model = ListOfConditions.Where(x => x.SelectedFieldToSearch.Equals(SelectFieldToSearch) && x.SelectedSearchCondition.Equals(SelectedSearchCondition) && x.SearchTerm.Equals(SearchTerm));
            return View(new AdvancedSearchModel());

        }
javascript c# asp.net-mvc razor model-binding
1个回答
0
投票

尝试一下:'@Url.Action("DisplayFilteredSearchResults", "FilteredSearch", new { ListOfConditions })',Url.Action需要使用一个具有名为ListOfConditions的属性的对象。 https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.urlhelper.action?view=aspnet-mvc-5.2#System_Web_Mvc_UrlHelper_Action_System_String_System_String_System_Object_

© www.soinside.com 2019 - 2024. All rights reserved.