?
Solved

mvc, onload?, asp.net

Posted on 2013-11-13
5
Medium Priority
?
1,938 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

762 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