2012-02-09 3 views
2

Html.ActionLink()를 사용하여 링크를 만듭니다. URL에서 얻은 쿼리 문자열의 조건에 따라 url에 매개 변수 문자열을 추가합니다. Concat string in Html.ActionLink()

<% 
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"입니다.

아무에게도 더 나은 해결책을 보여줄 수 있습니까?

고마워요.

답변

1

티티,

문제가 당신이 즉, routevalues ​​사전에 하나의 속성 '목적'을 추가하려고한다는 사실과 관련이 :이 경우

<%: Html.ActionLink("My link", "my_action", "my_controller", new {url_add} , null) %> 

, 당신은 추가 routevalue : new {url_add} 이것은 순전히 연결된 문자열입니다. 이 경로 값은 키 값 쌍이되어야하므로 단일 변수를 연결하고 추가하는 방식은 효과가 없습니다.

논리 흐름 내에서 '새로운'routevalues ​​사전을 작성하고 맨 마지막에 actionlink에만 추가하는 방법을 시도해 볼 것을 제안합니다 (즉, actionlink의 빌드가 한 번만 발생합니다 당신의 논리의 마지막 라인).

var newRoutes = new RouteValueDictionary(); 
// if condition for strA matches 
newRoutes.Add("aa", strA); 
// if condition for strb matches 
newRoutes.Add("bb", strB); 

희망이 몇 가지 아이디어가 있습니다.

[편집] - 아래에 귀하의 의견에 응답하여, 여기에 @class 개체를 포함하여 필요한 과부하가있다 :

<%: Html.ActionLink("My link", "my_action", "my_controller", newRoutes, new Dictionary<string, object> { { "class", "selectedQ" } }) %> 
+0

감사 짐. 어쨌든이 코드 블록을 Global.asax.cs에 넣어야합니까? – titi

+0

titi -이 작은 블록은 strA 변수 등을 정의하는 기존 코드에 추가 될 수 있습니다. –

+0

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