Hari Gillala
Hari Gillala

Reputation: 11916

action link mvc

How to open a new window with link to OutSide of MVC and take some Parameters while opening?

@Html.ActionLink("SomeTextForLink", "wwww.google.com", null, new { querystring values}, new { target ="_blank"})

I want to open something like www.abc.com/contact.aspx?id=22 This is outside of MVC Page

Thank you

Upvotes: 1

Views: 623

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could write a custom helper for this:

public static class HtmlExtensions
{
    public static IHtmlString Link(this HtmlHelper htmlHelper, string linkText, string baseUrl, object query, object htmlAttributes)
    {
        var anchor = new TagBuilder("a");
        anchor.SetInnerText(linkText);
        foreach (var item in new RouteValueDictionary(htmlAttributes))
        {
            anchor.Attributes[item.Key] = item.Value as string;
        }

        var urlBuilder = new UriBuilder(baseUrl);
        var values = HttpUtility.ParseQueryString(string.Empty);
        foreach (var item in new RouteValueDictionary(query))
        {
            values[item.Key] = item.Value as string;
        }
        urlBuilder.Query = values.ToString();
        anchor.Attributes["href"] = urlBuilder.ToString();

        return new HtmlString(anchor.ToString());
    }
}

and then:

@Html.Link(
    "SomeTextForLink",
    "http://www.google.com",
    new { param1 = "value1", param2 = "value2" }, 
    new { target = "_blank" }
)

Upvotes: 2

dknaack
dknaack

Reputation: 60438

You cant use an ActionLink for that. ActionLinks are only for links to an Action inside your Application.

You can use a usual link

<a href="@string.Format("http://www.abc.com/contact.aspx?{0}", yourQueryString)" target="_blank">
    Link Text
</a>

Upvotes: 6

Related Questions