使用 RouteValueDictionary 将字符串列表发送到控制器

Send a string list to the controller using RouteValueDictionary

我有一个搜索屏幕,它以分页列表的形式给出结果。在更改页面时,我需要将模型的值获取到控制器中的 GET 方法。虽然我能够传递作为字符串的模型属性,但我在传递字符串列表时遇到了问题。

查看代码:

<div class="divSearch">
<div class="divCriteria">
    <div class="row">
        <div class="col-md-6">
            @Html.LabelFor(m => m.Name)
            @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
        </div>
        <div class="col-md-6">
            @Html.LabelFor(m => m.Owner)
            @Html.TextBoxFor(m => m.Owner, new { @class = "form-control" })
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-md-6">
            @Html.LabelFor(m => m.County)
            @Html.ListBoxFor(model => model.County, Model.CountiesList, new { @class = "form-control", multiple = "multiple" })
        </div>
    </div>
    <br />
    <div class="row">
        <div class="right">
            <button type="submit" class="btn btn-primary"><i class="fa fa-share-square-o"></i>Search</button>
        </div>
    </div>
</div>

<div class="divResults">
    <div class="table-responsive">
        <table class="table table-hover table-advance dataTable">
            <thead>
                <tr>
                   <th style="display:none">ID</th>
                    <th>Name</th>
                    <th>Type</th>
                    <th>County</th>                       
                </tr>
            </thead>
            <tbody>

                @foreach (var item in Model.SearchList)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.Name)
                            @Html.HiddenFor(modelItem => item.ID)
                        </td>

                        <td>
                            @Html.DisplayFor(modelItem => item.Type)
                        </td>

                        <td>
                            @Html.DisplayFor(modelItem => item.County)
                        </td>                                                 
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>

@if (Model.SearchList != null)
{
var county = new List<string>();
foreach (var item in Model.County)
{
    county.Add(item);
}

@Html.PagedListPager(Model.SearchList, Page => Url.Action("Index", "FacilityFinder", new RouteValueDictionary() { { "Page", Page }, { "name", Model.Name },  { "owner", Model.Owner }, { "county", county} }),PagedListRenderOptions.PageNumbersOnly)
 }

控制器代码:

public ActionResult Index(int? page=null,string name = null, List<string> county=null,string owner = null)
{
}

name 和 owner 的值在控制器中没问题,但县列表给了我 System.Collections.Generic.List`1[System.String]

我是不是漏掉了什么?

您不能传递列表等复杂类型。您可能需要动态构建 RouteValueDictionary

var query = new RouteValueDictionary
{ 
    { "name", Model.Name },  
    { "owner", Model.Owner }
};

for (var i = 0; i < Model.County.Count; i++)
{
    query["county[" + i + "]"] = Model.County[i];
}

@Html.PagedListPager(
    Model.SearchList, 
    Page => Url.Action("Index", "FacilityFinder", new RouteValueDictionary(query) { { "Page", Page } }),
    PagedListRenderOptions.PageNumbersOnly
)

因此结果 url 看起来像这样:

/FacilityFinder/Index?Page=5&name=Foo&owner=Bar&county[0]=foo1&county[1]=foo2...

这将使 ASP.NET MVC 中的默认模型绑定器满意并将其正确绑定到 List<string>