Html.ActionLink()でリンクを作成します。 URLから取得するクエリ文字列の条件に基づいて、URLにパラメータ文字列を追加します。Html.ActionLink()内のconcat文字列
<%
strA = Request.QueryString["AA"];
strB = Request.QueryString["BB"];
strC = Request.QueryString["CC"];
if (!string.IsNullOrEmpty(strA))
{
%>
<%: Html.ActionLink(a.Name, Model.ActionName, Model.ControllerName,
new {aa = strA , tab = 2}, null)%>
<%
}else if(!string.IsNullOrEmpty(strB)){
%>
<%: Html.ActionLink(a.Name, Model.ActionName, Model.ControllerName,
new {bb = strB , tab = 2}, null)%>
<%
}else if(!string.IsNullOrEmpty(strA) && !string.IsNullOrEmpty(strB)){
%>
<%: Html.ActionLink(a.Name, Model.ActionName, Model.ControllerName,
new {aa = strA , bb = strB, tab = 2}, null)%>
<%else{ %>
<%: Html.ActionLink(a.Name, Model.ActionName, Model.ControllerName,
new {tab = 2}, null)%>
<% }%>
これは私が何をしようとしたものです:私は、文字列の準備ができCONCAT
<%
string url_add = "";
if (!string.IsNullOrEmpty(strA))
{
url_add += "aa=strA";
}else if(!string.IsNullOrEmpty(strB)){
url_add += "bb=strB";
}else if(!string.IsNullOrEmpty(strA) && !string.IsNullOrEmpty(strB)){
url_add += "aa=strA&bb=strB";
}else{
url_add += "tab=2";
}
%>
した後、私は以下のようにその文字列を置く:
<%: Html.ActionLink("My link", "my_action", "my_controller", new {url_add} , null) %>
しかし、私はこれをしなかったときは、私のURLは"blahblah.com/url_add=aa=strA"
です。
もっと良い解決方法を教えてください。
ありがとうございます。
おかげでジム。とにかく、このコードブロックをGlobal.asax.csに入れる必要がありますか? – titi
titi - この小さなブロックは、strA変数などを定義する既存のコードに追加することができます –
jim、私はリンク '%:Html.ActionLink(" My link "、" my_action "、" my_controller "、new {newRoutes}、null)%> 'が、URLは' blahblah.com/My_controller/My_action?newRoutes = System.Web.Routing.RouteValueDictionary'です。 – titi