tags:

views:

29

answers:

1

Imagine you've got n pages, each of which approximately shares the same sort of model but that model has to be in a particular state before you can access certain pages.

So if the user types in a URL to take them to page m, but this page is not accessible at the moment, the controller adds an error message to a collection of errors in TempData then redirects to page m-1.

The problem is when page m-1 is also not accessible. If we add a message to the same collection (with the same key) in TempData, we don't see it on page m-2 as it gets removed from TempData before the request for page m-2 gets underway.

I can imagine a solution where we have multiple error keys and each time we want to add an error or get errors back we try each key in turn, but has anyone got any better ideas? (I know that in theory I could work out the correct page to redirect to straight off but that is going to take a lot of rework and I don't have much time!)

EDIT:

This is the sort of thing I was thinking about:

    protected void AddError(string error)
    {
        int keyCounter;
        var errors = GetErrors(out keyCounter);

        errors.Add(error);

        TempData.Remove(GetKey(keyCounter + 1));
        TempData[GetKey(keyCounter + 1)] = errors;
    }

    protected List<string> GetErrors()
    {
        int jnk;
        return GetErrors(out jnk);
    }

    private string GetKey(int i)
    {
        return string.Format("ErrorKey:{0}", i);
    }

    private List<string> GetErrors(out int keyCounter)
    {
        keyCounter = 0;
        List<string> errors = null;

        for (int ii = 0; ii < MaxErrorKeyCounter; ii++)
        {
            string tryKey = GetKey(ii);
            if (TempData.ContainsKey(tryKey))
            {
                keyCounter = ii;
                errors = (List<string>)TempData[tryKey];
            }
        }

        if (errors == null)
            errors = new List<string>();

        return errors;
    }
A: 

Why not just use the Session?

Will
It just doesn't feel right. I'd have to manually empty it after putting it into my View Model. I think it turns out that I do after all know which page I can redirect to right off the bat.
Gaz