简体   繁体   中英

one controller value to another controller not going in asp.net mvc?

i am creating a controller name ProductController on there have a method

 public void DeliveryLocationCookies()
    {


            HttpCookie deliveryloccookies1 = Request.Cookies["deliveryloccookies"];
            if (deliveryloccookies1 != null)
            {
                ViewBag.deliveryloccookies = deliveryloccookies1["deliveryloccookies"];

            }



    }

Now i am creation another controller name HomeController on there i am creating a object of ProuctController

ProductController pc = new ProductController();

an creating one action result

public ActionResult view()
    {
        pc.DeliveryLocationCookies();

        return view();
    }

but from there produce exception

** System.NullReferenceException: Object reference not set to an instance of an object. at TestSite.Controllers.ProductController.DeliveryLocationCookies() in e:\\WINUSB\\TestSite\\TestSite\\Controllers\\ProductController.cs:line 353**

i cant understand that how can access other controller method even i dont know it's right process or not.

This happens because your controller needs to initializes itself. During this process it sets the Request property (that you are using) and others properties.

Take a look at this answer, it is similar to yours (and the solution too): Session is null when calling method from one controller to another...MVC

Regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM