2017-02-14 34 views
0

我有一個搜索屏幕,其中顯示了分頁列表中的結果。在更改頁面時,我需要將Model的值傳遞給控制器​​中的GET方法。雖然我能夠傳遞字符串的模型屬性,但我在傳遞字符串列表時遇到問題。使用RouteValueDictionary向控制器發送字符串列表

查看代碼:

<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]

我錯過了什麼嗎?

回答

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 
) 

使得所得到的URL看起來是這樣的:

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

這將使默認的模型綁定在ASP.NET MVC快樂和正確綁定這一個List<string>

+0

編譯錯誤:一個名爲'Page'的局部變量不能在這個範圍內聲明,因爲它會賦予'Page'不同的含義,'Page'已經在'父或當前'範圍內用來表示別的東西。 – TheFallenOne

+0

oops,'Page'是動態的,我用一個如何處理這個問題的例子更新了我的答案。 –

相關問題