Reputation: 13594
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>
Upvotes: 0
Views: 3886
Reputation: 1039588
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);
}
Upvotes: 1