views:

72

answers:

6

I have an ASP.NET MVC application. I have come to an idea of generating autoincremented values to be used as unique element ids. The question is, how can I have and work with a global variable which should be there for the duration of a request (page generation) but no longer?

I thought of using TempData for this shared variable and then just delete this key when the page is done. But then, where in code to purge this TempData key? Obviously it has to be some very last piece of code where the page has been rendered already.

Any input is highly appreciated.

EDIT: I have a number of HTML helpers that can be called from various views and partial views, so declaring a variable on a page and passing it to each helper is obviously not a good solution. I wish to just use the helpers and know they all are getting unique ids behind the scenes.

A: 

How about just an instance field in the page code-behind?

Matt Howells
A: 

If all you need is to store a number, the resources that would take to manage its lifestyle would take a lot more than just having a one static integer and always reusing it.

Do not bother deleting the key after each request. Just use a static (I think this is shared in visual basic) integer, use and increment it every time you need a unique value. Also take its mod with a ridiculously high number each time to make sure it will not be reused in a single request and it will never overflow.

Serhat Özgel
static would mean it's shared by all users of the site. Reading further into the question, I don't think that's what he wants.
Joel Coehoorn
From what I read, all he wants is a unique number for element id's. It won't matter if the numbers are shared (in this case one variable is shared but the values retrieved from it will definitely be unique) or not.
Serhat Özgel
A: 

Why don't you define your integer variable at the top of the page view file? Use it throughout the view rendering execution and at the end of it you can easily leave it as is. You don't have to explicitly destroy anything. Your variables live for the duration of request only. IIS is stateless service (if you subtract Session, Cache and Application variables) so it doesn't really remember anything explicitly.

Robert Koritnik
There are helper methods that cannot have access to view unless I pass this variable to each method and to partial view explicitly. I don't really want to add one more parameter to each and every call.
User
Fair. Thanks for the extra explanation.
Robert Koritnik
A: 

I would imagine you could use the Application_BeginRequest and Application_EndRequest methods in global.asax.cs; Note I can't double check the method names currently, but I think they are close.

Chris Shaffer
A: 

You could create a member variable in your controller which would be regenerated for each request:

public class ItemController : Controller 
{

    private int _UniqueID = 0;

    public ActionResult Index()  
    {
         foreach (var item in items)
         {
              item.UniqueID = _UniqueID++;
         }
         // etc...
    }
John Rasch
Controller doesn't do anything to your view. It just provides view data and doesn't (and can't) manipulate view's controls/html, since we don't really have controls in MVC as there are in web forms.
Robert Koritnik
That is correct - I'm just assuming each of his controls are being generated based on some individual data item. I'm not sold on the idea that View should be doing anything (tracking state, creating unique ids, etc.) other than displaying what's explicitly passed to it
John Rasch
+1  A: 

Okay, I have googled a little bit and found a solution on ASP.NET forums.

http://forums.asp.net/t/1401685.aspx

Obviously, I can use the HttpContext.Current.Items collection to have my little static variable for the duration of a request.

User
Do you need to use this variable outside view rendering execution sequence? If not, I don't really see why bother using this collection.
Robert Koritnik
What else could I use instead?
User