1

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.

Adalcar
  • 1,458
  • 11
  • 26
Ash_4416
  • 11
  • 3
  • did you try like this ` return RedirectToAction("name_of_view");` – Saif Oct 12 '17 at 08:46
  • yes but it shows the view in the same controller. The previous programmer gave same names to the views but for different controller – Ash_4416 Oct 12 '17 at 09:10
  • Can you provide complete example code? `return RedirectToAction("Add","ReasonTree");` seems valid when `Add` is action name & `ReasonTree` belongs to controller name. Is `return View("Add")` just enough? – Tetsuya Yamamoto Oct 12 '17 at 09:10
  • I may not able to provide the complete code can paste little code snippet. – Ash_4416 Oct 12 '17 at 10:23
  • Are you sure that you spelled the Controller and the method names correctly? And are you sure that your controller method is written in the correct controller class? return RedirectToAction("ActionOrViewName", "ControllerName"); works fine for me. – Tharushi Geethma Oct 13 '17 at 04:47

2 Answers2

1

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.

Tharushi Geethma
  • 1,249
  • 15
  • 21
  • 1
    yes the controller name and methodname is correct. The previously written code was running properly I didnt make changes in original code I am just try to call the methods and view. Also one thing in the previous code is method overloding is there in controller code. so same method name with different arguments. – Ash_4416 Oct 13 '17 at 06:45
  • Did you try **clearing cache** in your web browser? Although the method is overloaded,there should not be any flows and should work fine. – Tharushi Geethma Oct 13 '17 at 08:46
0

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.

rased
  • 136
  • 2
  • 6
  • Thanks for your reply, as you mentioned in your reply for first case if it strictly bind to model what should I do in that case. Another thing is view is bind some model which does contains the argument I need to pass for the method, I tried to do the same way mention but gives error. How to access that other model so that I can pass the arguments to the method so I am stuck here – Ash_4416 Oct 18 '17 at 07:28
  • Your question is not clear to me. Let me see your Action methods and view. – rased Oct 18 '17 at 07:42