开发者

How to record a click and pass the user onto the link with MVC3, C# and Razor?

开发者 https://www.devze.com 2023-03-13 06:01 出处:网络
When a user clicks on a link on my page, I want to be able to record that link click then send the user off to that link in another window.

When a user clicks on a link on my page, I want to be able to record that link click then send the user off to that link in another window.

Given the following ActionLink:

@Html.ActionLink(Model.dsResults.Tables[0].Rows[i]["title"].ToString(), "LinkClick", new { itemListID = @Model.dsResults.Tables[0].Rows[i]["ItemListID"] })

The LinkClick ActionResult will be called with the ItemListID past to it. However, I don't want to go back through my model/db to get the link. Can I pass a DataRow type and get the cell c开发者_运维百科ontaining the link and other details, and secondly, once I have saved the link click, how do I redirect the user, in a new window, to that link?

Are there Razor HTML helpers for this kind of thing?

Thanks.

UPDATE

Seems that I've forgotten some of the basics... while the discussion with Malcolm regarding using caching holds some value, how about the following:

In the past, what I would do is link to an internal page which would open _blank, take in the ID, do the DB call to save, then redirect the user in that window to the appropriate URL.

I think this clarifies the issue a lot.


There is no such helper available. I couldn't 100% follow how you want this scenario to work, but the idea is that you should use jQuery to attach to the click event, call some tracking url via .ajax, and then return so the original link click is processed.


//(this looks somewhat ugly - the point of this is the id parameter at the end to set the link's id)
@Html.ActionLink(Model.dsResults.Tables[0].Rows[i]["title"].ToString(), "LinkClick", new { itemListID = @Model.dsResults.Tables[0].Rows[i]["ItemListID"], id="sweetLink" })

Then your javascript block somewhere after the link:



$('#sweetLink').click(function() {
  .ajax( your call here to record the click - ); //use $(this).whatever to access your link properties
   return true;
});

When the call returns the original link url will be processed.


It depends what you are willing to expose to the user - It is easy enough to include a url as a query string parameter in the ActionLink, and redirect to it after recording the link, but obviously that url will be visible to (and editable by) the user. If you want only the id to be visible you will have to go back to the DB (or a cache) - HTTP is stateless.

If the URLs are within your site, you have a a few other options - you could add a source link ID to a regular action link and use a global action filter to record where links to a particular action came from.

The actual redirect / new window is easy enough - set the target html attribute in your action link, and have the RecordLink action method return a RedirectResult.


Because you talk about DataRow I guess you come from ASP.NET and think about implementing a behaviour similiar to clicking a Button inside a Datagrid.

You get close to this behaviour by posting a value containing the data you need to and action an redirecting to the target action after that. Use the overload

RouteValueDictionary linkRouteValues = ControllerContext.RouteValues;

linkRouteValues.Add("LinkName","Link to Mama"); linkRouteValues.Add("LinkID","10");

RedirectToAction("LinkDisplay",linkRouteValues);

if you need to pass parameters to the target action.

This is a commonly used pattern - post-redirect-get.

This means you would need a button and no link.

Maybe rethink your wish to not to go back to the model and db (or better cache) after a link is clicked. This would be the way most ASP.NET MVC developer choose.

0

精彩评论

暂无评论...
验证码 换一张
取 消