0

I have numerous link in my View, which are shown like below

retailerName1----------info----------anchor-Link-retailer1

retailerName2----------info----------anchor-Link-retailer2

retailerName3----------info----------anchor-Link-retailer3

Now, what I want is to capture the click from user on any of those links and then at server, I want to increase the clickCount field for that particular retailer record in the database.

For this, I suppose, I need some kind of client script like jquery code which should send the rerailer data and then use this data at server to do further operation. Please tell me the jquery code for this which will send the appropriate data.

UPDATE

this is the link code

<a href="retailerURL.com" target ="_blank">anchor-Link-retailer3</a>

1 Answer 1

1

It's not quite clear what you call appropriate data in your question, but doesn't the retail id suffice? You don't need javascript for this. When generating your link, you could pass along this retailer id:

@foreach (var retailer in Model.Retailers)
{
    <div>
    @retailer.Name

    ... some info

    @Html.ActionLink(
        "some link text", 
        "someAction", 
        "someController", 
        new { retailerId = retailer.Id },
        null
    )
    </div>
}

and inside the corresponding controller action you could use this retailer id to do whatever you were intending to do:

public ActionResult SomeAction(int retailerId)
{
    ...
}

UPDATE:

Once possibility is to use an AJAX request to send the retailer id to your controller action when the link is clicked. For that you will need the retailer id. You could use an HTML5 data-* attribute on the anchor:

<a href="retailerURL.com" target ="_blank" class="retailer" data-retid="123" data-url="@Url.Action("SomeAction")">anchor-Link-retailer3</a>

and then you could use jQuery to subscribe to the click event of all those links and send the AJAX request:

$(function() {
    $('.retailer').click(function() {
        var url = $(this).data('url');
        var retailerId = $(this).data('retid');
        $.post(url, { retailerId: retailerId });
    });
});

UPDATE 2:

Another possibility is to perform the redirect to the retailer website inside your controller action. This way you don't need AJAX:

@Html.ActionLink(
    "some link text", 
    "someAction", 
    "someController", 
    new { retailerId = retailer.Id },
    new { target = "_blank" }
)

and inside the controller action update the database, fetch the retailer website and redirect:

public ActionResult SomeAction(int retailerId)
{
    // TODO: 
    // 1. fetch the retailer website given the id:
    // 2. update the database with the info
    // 3. redirect to the retailer website

    string retailerWebsite = ...
    return Redirect(retailerWebsite);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Shit !! may be I didn't explained the thing clearly.... The link opens a new window with retailer website....I will update the question and show the code for link too.....extremely sorry for misleading....
@PankajUpadhyay, OK, now it's more clear. I have updated my answer with an example which would use an AJAX request to call some controller action and pass it the retailer id so that you can update the database.
thanks darin, it solved the problem !! In future will twice check the question. :-)
@PankajUpadhyay, I have updated my answer and added another possible solution in case you don't want to use javascript. Personally I would recommend it.
@PankajUpadhyay, yes, that's exactly what the target="_blank" html attribute does. It is added to the anchor, so the user won't leave your site. When he clicks on the link, a new tab/window will be opened and the controller action executed. At the end it will simply redirect to the target site inside this tab/window.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.