1

I have a facebook share button in my View,

<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>

So When a user clicks on It I want to save some data in Shares Tableof my Database, lets say i want to call controller method "https://localhost:5001/venue/share?vid=88.

In my Controller i have

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();
        }
Not A Bot
  • 2,474
  • 2
  • 16
  • 33
MADMAX
  • 152
  • 1
  • 3
  • 17

2 Answers2

0

You can use JQuery and AJAX to post data to controller to save data.


When user click button, trigger the Javascript function to get data-href and post data to backend.

ps. don't forget to include jquery library.

Simply example:

FBShare View :

<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>

Controller :

public IActionResult FBShare()
{
    return View();
}

[HttpPost]
public void SaveShare(string url)
{
    Console.WriteLine(url);
    // Save url data
}
Vic
  • 758
  • 2
  • 15
  • 31
  • the controller action method doesnt get called. I tried it but still no luck,, ;-( – MADMAX Jan 30 '20 at 09:28
  • @markharringson did you try to use debug mode ? This code work find . Or you can past your code or minimal project – Vic Jan 30 '20 at 09:31
  • @markharringson You can create a new project , paste my demo code , it work fine. – Vic Jan 30 '20 at 09:31
  • there is no controller name in ``` $.post("SaveShare", { url: data_href } ); ``` – MADMAX Jan 30 '20 at 10:39
  • @markharringson Yes, you can replace it to your own function name. – Vic Jan 30 '20 at 11:50
0

You can use 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>
A.R.SEIF
  • 865
  • 1
  • 7
  • 25
  • thanks, but i am not sure why i am not getting any response. this is not working, what may i have missed? – MADMAX Jan 30 '20 at 09:27
  • See if your controller is called. Do this by placing breakpoints on the controller side – A.R.SEIF Jan 30 '20 at 09:32
  • Is venueController in controller folder?Or In Area?also trace in console browser and watch happen?if Use HttpPOST in controller change xhttp "GET" to "POST" – A.R.SEIF Jan 30 '20 at 10:32
  • 1
    it works when i type https://localhost:5001/Venue/Share?vid=1 in browser – MADMAX Jan 30 '20 at 10:54
  • very good.Typed xhttp.open("GET", "/Venue/Share?vid=" + vid, true);and also console.log and see call a function in console browser? – A.R.SEIF Jan 30 '20 at 11:11
  • My action is not getting hit with `` but it gets hit on ` ` – MADMAX Jan 31 '20 at 03:53
  • ` Share ` and not reply follow in [link](https://stackoverflow.com/questions/7347786/html-anchor-tag-with-javascript-onclick-event) – A.R.SEIF Feb 03 '20 at 07:52