0

I'm in serious need of passing url params with View class. Here's code:

            if (!ModelState.IsValid)
            {
                return View(model);
            }

This should not only return model based view, but also add specific param to URL (param won't change view details, but is needed as it's one of few automatically generated SessionKeys (one for each tab/window used to view app) and I know no other way to get to it, different than passing as param (it can't be generated everytime, 'cos params will change; it can't be global variable because it'll reset its value each refresh; it can't be static, because static is evul).

Oh this action is called with use of form and submit button, not actionLink or something like this.

EDIT1: I need params to stay in URL after refresh, or I need some other form of keeping data that persists through refresh/validation fail.

1
  • "one for each tab/window used to view app" ain't cookies browser-wide? And how will I know cookie's ID for like 10th tab? I mean how will I know it's 10th, not 5th tab? Commented Jun 21, 2012 at 7:22

2 Answers 2

1

If I understand you correctly you have data that you need to use in generating Urls on your page? This just forms part of your ViewModel - or at least it should, since it's data that the View needs in order to render.

You can use ViewData to add any extra data that isn't part of your view model. Or, better still, add the data as members to it. Equally, if different views with different View Models require this data, add a ViewModel base class and derive from that so you can share that data.

Sign up to request clarification or add additional context in comments.

3 Comments

Again, this requires me to know ID for ViewData, or is this tab/instance unique? I mean it won't be shared if I open 2 tabs of same page? And furthermore, how can I generate URL from this? Won't it reset after refresh?
Well, you look for the value - if it's not there you generate it and add it to all subsequent urls. If it is there you re-use it for all urls. Sounds to me like this is just non-cookie-based session where the session ID goes in the query string.
But this won't last through validation fail/refresh. I need to have same data after validation fail. Guess I'll have to expand model.
1

use

RedirectToAction("actionName","controller",
     new RouteValueDictionary(new {param1="value",param2="value2"});

or you can use hidden field to store the values in your page and then pass this down as and when you need them..

4 Comments

All cool, but as you can see it happens on validation fail. With this no validation fail info is given. And it resets whole form.
huh?? Why cant your redirected action handle this? You can pass data to this action... Using TempData is one of the ways!
That's great idea! But... will it work with 2 tabs? I mean will these tabs have 2 different TempData objects?
yea.. tempdata lasts across one request redirecttoaction.. read up on it

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.