【问题标题】:How can I call controller method with anchor tag that has "href" attribute?如何使用具有“href”属性的锚标记调用控制器方法?
【发布时间】:2020-05-15 18:38:15
【问题描述】:

我的视图中有一个 Facebook 分享按钮,

<div class="fb-share-button" 
     data-href="https://localhost:5001/venue/details/@Model.Id"
     data-layout="button_count" data-size="large">
  <a href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Flocalhost%3A5001%2Fvenue%2Fdetails%2F@Model.Id%3Fq%3Dgaurav-acharya&amp;src=sdkpreparse" class="fb-xfbml-parse-ignore">
    Share
  </a>
</div>

所以当用户点击它时,我想在我的数据库的共享表中保存一些数据,假设我想调用控制器方法“https://localhost:5001/venue/share?vid=88。

在我的控制器中,我有

public void Share(int vid)
        {
            Share share = new Share();
            share.UserName = GetUserName(_userManager);
            share.VenueId = vid;
            Console.WriteLine(vid);
            _context.Shares.Add(share);
            _context.SaveChanges();
        }

【问题讨论】:

    标签: javascript html asp.net-mvc model-view-controller .net-core


    【解决方案1】:

    您可以使用 JQuery 和 AJAX 将数据发布到控制器以保存数据。


    当用户点击按钮时,触发Javascript函数获取data-href并将数据发布到后端。

    ps。不要忘记包含 jquery 库。

    简单示例:

    FBShare 视图:

    <h1>FBShare</h1>
    
    <div name ="shareButton"
         class="fb-share-button" 
         data-href="https://localhost:5001/venue/details/100"
         data-layout="button_count" data-size="large">
        <a href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Flocalhost%3A5001%2Fvenue%2Fdetails%2F@Model.Id%3Fq%3Dgaurav-acharya&amp;src=sdkpreparse" class="fb-xfbml-parse-ignore">
            Share
        </a>
    </div>
    <script
        src="https://code.jquery.com/jquery-3.4.1.min.js"
        crossorigin="anonymous"></script>
    <script>
        $('[name="shareButton"]').on("click",saveData);
        function saveData() {
            var data_href = $('[name="shareButton"]').attr('data-href');
            $.post("SaveShare", { url: data_href } );
        }
    </script>
    

    控制器:

    public IActionResult FBShare()
    {
        return View();
    }
    
    [HttpPost]
    public void SaveShare(string url)
    {
        Console.WriteLine(url);
        // Save url data
    }
    

    【讨论】:

    • 控制器操作方法没有被调用。我试过了,但仍然没有运气,;-(
    • @markharringson 您是否尝试使用调试模式?此代码工作查找。或者你可以过去你的代码或最小的项目
    • @markharringson 您可以新建一个项目,粘贴我的演示代码,它工作正常。
    • ``` $.post("SaveShare", { url: data_href } ) 中没有控制器名称; ```
    • @markharringson 是的,你可以把它替换成你自己的函数名。
    【解决方案2】:

    你可以使用 ajax。

    <div class="fb-share-button" 
     data-href="https://localhost:5001/venue/details/@Model.Id"
     data-layout="button_count" data-size="large">
     <a id="fbLink" onclick="saveInfo(@Model.Id)" class="fb-xfbml-parse-ignore">
    Share
     </a>
    </div>
    <script>
      function saveInfo(int vid){
        var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
         if (this.readyState == 4 && this.status == 200) {
            location.replace("https://www.facebook.com/sharer/sharer.php? 
            u=https%3A%2F%2Flocalhost%3A5001%2Fvenue%2Fdetails%2F@Model.Id%3Fq%3Dgaurav- 
            acharya&amp;src=sdkpreparse")
           }
         };
     xhttp.open("GET", "/venue/Share?vid=" + vid, true);
     xhttp.send();
     }
    </script>
    

    【讨论】:

    • 谢谢,但我不知道为什么我没有得到任何回应。这不起作用,我可能错过了什么?
    • 查看你的控制器是否被调用。通过在控制器端放置断点来做到这一点
    • 场地控制器在控制器文件夹中吗?还是在区域中?也在控制台浏览器中跟踪并观察发生的情况?如果在控制器中使用 HttpPOST 将 xhttp "GET" 更改为 "POST"
    • 当我在浏览器中输入localhost:5001/Venue/Share?vid=1 时它可以工作
    • 非常好。键入 xhttp.open("GET", "/Venue/Share?vid=" + vid, true);还有console.log 并查看控制台浏览器中的调用函数?
    猜你喜欢
    • 2013-12-29
    • 2012-05-17
    • 2021-11-12
    • 1970-01-01
    • 2021-09-23
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    相关资源
    最近更新 更多