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

时间:2017-02-14 16:12:15

标签: c# asp.net-mvc pagedlist

我有一个搜索屏幕,可以在分页列表中显示结果。在更改页面时,我需要将模型的值获取到控制器中的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)
{
}

控制器中名称和所有者的值很好,但是县列表给了我System.Collections.Generic.List`1 [System.String]

我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

您无法传递复杂类型,例如列表。您可能需要动态构建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
)

以便生成的网址如下所示:

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

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