Ash_4416
Ash_4416

Reputation: 11

calling view of one controller from another controller

I am working with ASP and MVC5. I am trying to call view of one controller from another controller. I am trying to use method as mentioned in following link

return View("../ReasonTree/Add");

But it throws a systemnull reference exception.

I tried this method also:

return RedirectToAction("Add","ReasonTree");

There again it throws a System.ArgumentException

I have to work on already written code and makes changes in that code.

Upvotes: 1

Views: 875

Answers (2)

rased
rased

Reputation: 136

If you return a view which is strictly bind to any model it gives Null Reference Exception. Which is happening at your first return example return View("../ReasonTree/Add"); . And in your second example return RedirectToAction("Add","ReasonTree"); I think Add method takes argument and you are not supplying any argument. Thats why its giving ArgumentException. Supply the argument with your redirectToAction, like return RedirectToAction("Action","controller", new {@id=id}); . Hope it will work fine.

Upvotes: 0

Tharushi Geethma
Tharushi Geethma

Reputation: 1289

If it throws a systemnull reference exception,then either the Controller name or the method name might be inaccurate.Or you have written the controller method inside a wrong controller class.

return RedirectToAction("ActionOrViewName", "ControllerName");

works fine for me.

Upvotes: 1

Related Questions