C# 使用RouteValueDictionary向控制器发送字符串列表
我有一个搜索屏幕,它以分页列表的形式给出搜索结果。在更改页面时,我需要将模型的值获取到控制器中的get方法。虽然我能够传递作为字符串的模型属性,但传递字符串列表时遇到了一个问题 查看代码:C# 使用RouteValueDictionary向控制器发送字符串列表,c#,asp.net-mvc,pagedlist,C#,Asp.net Mvc,Pagedlist,我有一个搜索屏幕,它以分页列表的形式给出搜索结果。在更改页面时,我需要将模型的值获取到控制器中的get方法。虽然我能够传递作为字符串的模型属性,但传递字符串列表时遇到了一个问题 查看代码: <div class="divSearch"> <div class="divCriteria"> <div class="row"> <div class="col-md-6"> @Html.LabelFor(m
<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)
}
@LabelFor(m=>m.Name)
@TextBoxFor(m=>m.Name,新的{@class=“form control”})
@LabelFor(m=>m.Owner)
@TextBoxFor(m=>m.Owner,新的{@class=“form control”})
@LabelFor(m=>m.County)
@Html.ListBoxFor(model=>model.County,model.CountiesList,新的{@class=“formcontrol”,multiple=“multiple”})
搜寻
身份证件
名称
类型
县
@foreach(Model.SearchList中的var项)
{
@DisplayFor(modelItem=>item.Name)
@Html.HiddenFor(modeleItem=>item.ID)
@DisplayFor(modelItem=>item.Type)
@DisplayFor(modelItem=>item.County)
}
@if(Model.SearchList!=null)
{
var county=新列表();
foreach(Model.country中的var项)
{
增加(项目);
}
@Html.PagedListPager(Model.SearchList,Page=>Url.Action(“Index”,“FacilityFinder”,newRouteValueDictionary(){{“Page”,Page},{“name”,Model.name},{“owner”,Model.owner},{“country”,country}),pagedListRenderations.PageNumbersOnly)
}
控制器代码:
public ActionResult Index(int? page=null,string name = null, List<string> county=null,string owner = null)
{
}
public ActionResult索引(int?page=null,string name=null,List county=null,string owner=null)
{
}
name和owner的值在控制器中没有问题,但是county的列表给了我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
)
这将使ASP.NET MVC中的默认模型绑定器满意,并将其正确绑定到
列表
您无法传递复杂类型,如列表。您可能需要动态构建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
)
这将使ASP.NET MVC中的默认模型绑定器满意,并将其正确绑定到
列表
编译错误:无法在此范围内声明名为“Page”的局部变量,因为它将赋予“Page”不同的含义,而“Page”已在“parent or current”范围内用于表示其他内容。oops,Page
是动态的,我已经用一个如何处理这个问题的示例更新了我的答案。编译错误:名为“Page”的局部变量不能在此范围内声明,因为它将赋予“Page”不同的含义,而“Page”已在“父级或当前”范围内用于表示其他内容。哎呀,Page
是动态的,我用一个例子更新了我的答案,说明了如何处理这个问题。