Solved

mvc, onload?, asp.net

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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