我想使用Html.ActionLink,它允许为查询字符串中使用的名称/值对传递路由值,其名称由变量定义。
@{
var searchFieldName = ViewData["SearchFieldName"];
var searchFieldValue = ViewData["SearchFieldValue"];
string searchView = ViewData["ReturnSearchView"].ToString();
}
@Html.ActionLink("Search", searchView, new { searchFieldName = searchFieldValue });
我希望searchFieldName替换为它的值,例如:
<a href="/search?somefield=value">
相反,查询字符串具有&#34; searchFieldName&#34;作为名称:
<a href="/search?searchFieldName=value">
如何达到预期效果?
答案 0 :(得分:2)
您可以使用@Html.ActionLink()
overload accepting a RouteValueDictionary
,因此您可以使用字符串指定密钥的名称:
@{
var searchFieldName = ViewData["SearchFieldName"];
var searchFieldValue = ViewData["SearchFieldValue"];
var routeValues = new RouteValueDictionary();
routeValues.Add(searchFieldName, searchFieldValue);
string searchView = ViewData["ReturnSearchView"].ToString();
}
@Html.ActionLink("Search", searchView, routeValues, null);