Solved

mvc, onload?, asp.net

Posted on 2013-11-13
5
1,723 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
  • 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 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now