Solved

mvc, onload?, asp.net

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
2 questions 10 25
jquery datatables, asp.net MVC 9 41
Send SMS from VB via Sierra Wireless Modem 2 22
RLDC Reporting in Visual studio 11 16
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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