2016-03-08 76 views
0

I have implement a link list generate in view and each link is redirect to different website, when i click the link, it will redirect to the format as below:MVC <a href> redirect to other website?

http://localhost:19150/ModuleView/www.google.com

我想要什麼:

www.google.com

我的代碼:

@for (int x = 0; x < Model[i].Url_Address.Count(); x++) 
{ 
    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne"> 
     <div class="panel-body"> 
      <a href="@Model[i].Url_Address[x]" class="list-group-item">@Model[i].Url_Name[x]</a> 
     </div> 
    </div> 
} 

是,我需要先通過它的動作在控制器中重定向到網頁?如果我錯了,請糾正我。高度讚賞!

+0

不,noo需要那個,你在做什麼看起來正確 –

回答

1

您的鏈接需要以「http://」開頭,以便將其識別爲外部URL。

你可以這樣做:

string extLink = @Model[i].Url_Address[x]; 
if(!extLink.StartsWith(@"http://")) { 
    extLink = @"http://" + extLink; 
} 

......

<a href="@extLink" class="list-group-item">@Model[i].Url_Name[x]</a> 

注:這是隻用於演示目的,有更優化的方式來做到這一點(即做這在服務器端),但你應該明白。

+1

非常感謝信息!有用!我會用這個,直到我找到另一個好方法去做。 –

+0

@ Edward.K很高興聽到它 - 祝你好運! – wazdev

相關問題