Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

mvc, onload?, asp.net

Posted on 2013-11-13
5
Medium Priority
?
1,978 Views
Last Modified: 2013-11-22
I have below cookie function that need to be execute in onload event. And I use mvc 3 and there is no on load event exist, and so I put it within action result in home controller, and expect it will work. Unfortunately, it does not. I need some experts to help me
what to do.

Basically, all i want is to read cookie if cookie exists, do something like below.
and redirect url to some where else. The code is very simple but just don't know where it should stay when the page is loaded....

 
 public ActionResult Index()
        {
            //read cookie for the zipcode
            string memberZip = Server.HtmlEncode(Request.Cookies["memberZipCode"].Value);
            if (!String.IsNullOrEmpty(memberZip))
            {
                Decimal[] y = ServCommon.GetLocationConverter(memberZip);

                decimal loc1 = y[0];
                decimal loc2 = y[1];
                string x = ServSuretyNetworkRef.GetAgencyListByDistance(loc1, loc2, memberZip, 1, "1");
                string locationResult = x;
                Response.Redirect("/home?locations=" + locationResult);
            }
            return View();
        }
0
Comment
Question by:solution1368
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39647308
Which part are you having issues with? Is it setting the cookie itself, retrieving the value, or doing the redirect?
0
 
LVL 22

Expert Comment

by:ambience
ID: 39647550
You will have to provide further information like whether the code isn't working for this action or that you don't want to duplicate it for each action - confusion surrounding lack of onload().

In MVC, such code is a nice candidate for writing in an action Filter. See this example: http://msdn.microsoft.com/en-us/library/dd381609%28v=vs.100%29.aspx

filterContext.HttpContext can provide you with Request and cookies.

You can apply attribute at controller or action level and specified filters would be executed as part of request processing. This way the logic stays in one place.
0
 

Author Comment

by:solution1368
ID: 39648454
i need actual codes sample. Thanks
0
 

Author Comment

by:solution1368
ID: 39648475
no issue on cookie or redirect. I need to know how to put the method in on_load event in mvc 3. I know there is no on_load event in mvc 3. Something similar to on_load event will be fine. whatever you want to name.
0
 
LVL 22

Accepted Solution

by:
ambience earned 2000 total points
ID: 39652132
You should read on the differences between MVC and WebForms to develop better understanding. In MVC there is no such concept as Page Load because there is no page controller. If you want to you can write your own page controller framework over the core MVC framework but that is not easily justified.

Controllers and Actions are the central objects in MVC paradigm. A controller is instantiated for each request that maps to an action - some code can be put in a Controller's constructor but that is not the right place to add logic for redirects.

I've already mentioned ActionFilters as the MVC way of achieving the same affect as in your example. See the links in my previous post and let us know if you need further help.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question