Tanwer
Tanwer

Reputation: 1583

Redirecting to Action Result Using Href Attribute

I am trying to Invoke action result from an anchor tag,

I am building html into C# , its not in view and injecting it into view using html helper @Html.Raw(Model.HTML) like this.

HTML is

html.AppendLine("<td><a href='Home/DeleteFile?ID="+ dt.Rows[i]["ID"].ToString() + "'><i style='color:#e90029' class='fa fa-times fa-lg' aria-hidden='true'></i></a></td>");

The Problem is when i click first time on this anchor tag to delete the record it works by invoking address

http://WebsiteAlias/Home/DeleteFile?ID=2

But after that when i hover to another record to delete them it show URL to

http://WebsiteAlias/Home/Home/DeleteFile?ID=2

It add Another /Home in url ,

Why it is happening ?

I cant use @Url.Action("Action","Controller") cause HTML is coming from c#

Upvotes: 0

Views: 671

Answers (1)

Georg Patscheider
Georg Patscheider

Reputation: 9463

If the C# code that builds the HTML has access to the HttpContext, you can create an UrlHelper like this:

using System.Web;

var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
string url = urlHelper.Action("DeleteFile", "Home", new { ID = dt.Rows[i]["ID"].ToString() });
return url;

Edit: if you only have access to HttpContextBase, you can get the current HttpContext like this:

HttpContextBase contextBase;
HttpContext httpContext = contextBase.ApplicationInstance.Context;

Upvotes: 2

Related Questions