简体   繁体   English

从操作方法单击时获取锚标记 href

[英]getting anchor tag href on click from Action Method

I have a grid which includes below hyperlink row,currently for all rows we have same hyperlink and only ID is changing and it is working fine.我有一个网格,其中包含下面的超链接行,目前对于所有行,我们都有相同的超链接,只有 ID 发生变化,并且工作正常。

<a href=" + @ViewBag.Url+ ID + " target='_blank'>Test</a>

Now for every row, we have different link url which i would get from action method when I pass ID.现在,对于每一行,我们都有不同的链接 url,当我传递 ID 时,我会从 action 方法中获得这些链接 url。 I want to call MVC Action Method to get hyperlink url and then open it in another tab.How can I accomplish this?我想调用 MVC 操作方法来获取超链接 url,然后在另一个选项卡中打开它。我怎样才能做到这一点? I tried this one but it is not opening hyperlink?我试过这个,但它没有打开超链接?

<div class="row">
    <div class="col-md-4">
        <a href="@Url.Action("GetUrl", "Home",new { id = 1 })">Click Here</a>;
    </div>
</div>

 public string GetPDFUrl(string id)
        {

            return "test.com" + id;
        }

There are several ways to solve your problem.有几种方法可以解决您的问题。 one of them is using child actions.其中之一是使用子操作。 Put your generating URL part into a partial view to put your logic in your action method.将生成的 URL 部分放入局部视图,以将逻辑放入操作方法中。 So, create a child action method that can only be called in your views.因此,创建一个只能在您的视图中调用的子操作方法。

    [ChildActionOnly]
    public ActionResult GenerateUrlPartial(int id)
    {
        var generatedUrl = "";//your url business is here
        var model = new UrlInfo { Url = generatedUrl };
        return PartialView(model);
    }

Then, create GenerateUrlPartial.cshtml partial view :然后,创建GenerateUrlPartial.cshtml局部视图:

@model UrlInfo
@{
   Layout = null;
   ViewBag.Title = "GenerateUrlPartial";
 }

<div class="row">
    <div class="col-md-4">
        <a href="@Model.Url">Click Here</a>;
    </div>
</div>

And in your loop, call the action method like this :在你的循环中,像这样调用 action 方法:

@for (int i = 0; i < 10; i++)
{
     Html.RenderAction("GenerateUrlPartial", new { id = i });
}

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM