Is there any issue with creating a new controller within the ascx view pages. I don't want to create a model, are there any issues with doing it this way.

Normally, the controllers control variables within the model, and the model is the middle point between them, but I am creating a new instance of a controller class within the ascx page itself. Is this ok?

**EDIT I want to know the implications of putting an instance of a controller class directly into the asp.net ascx code on a view page


closed as off-topic by GlenH7, gnat, user40980, user22815, jwenting Aug 28 '14 at 8:28

  • This question does not appear to be about software engineering within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Unclear what help you need. Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the How to Ask page for help clarifying this question. – gnat Aug 26 '14 at 13:54
  • I'm also utterly baffled - what exactly are you trying to get from this new instance? – Jon Malcolm Aug 26 '14 at 14:07
  • Due to the lack of a model, I am retrieving a saved variable. – 2b77bee6-5445-4c77-b1eb-4df3e5 Aug 26 '14 at 14:09
  • You are building a hack. Don't do it. You are programming by permutation and observing the results and thinking that it produces what you want. This is exactly how spaghetti code happens. Somewhere down the line you will be asking another question about "how do I get the http context from my controller that I created explicitly?" and "why can't I access cookies from my view?" and so on – Chris McCall Aug 26 '14 at 20:03

Here, use one of these (ViewBag, ViewData, etc):


If you can't stick with the pattern at least work within it...

  • +1 There are several ways to get a variable into a view from a controller; you should use on of them. – Eric King Aug 26 '14 at 20:10
  • Cant believe I didn't think of this. TY – 2b77bee6-5445-4c77-b1eb-4df3e5 Aug 27 '14 at 14:40

Your basically asking what the implications of breaking the MVC pattern are. You can write an application in anyway you want. However, the advantage of using a pattern like MVC is that it is widely known. Any programmer who understands MVC can look at your application and say "Yes, I understand how the data flows throughout your application and I can add features to this existing application." If you go off book and start using an unrecognized pattern then only you will know how it works. You will have to spend time training someone else to learn how to modify your code. Another downside is if you need help with your application, then your going to have to find someone who understands your code before they can help you.

If you've already written code using the MVC pattern, then you should continue to do so. Changing the method by which data is being passed around your application after you have begun writing it is a bad idea. The code will be confusing to anyone else and probably you after not working on it for a couple months.

If you are creating an instance of a controller class in your view, then you will need to test that. This may be a pain to test. I don't know how you are planning to use the controller once instantiated, but if your planning to do any logic in your view, then it will be difficult to test. There is a reason that the controller modifies the view and not the other way around. It's easier to test a controller than it is a view.

Those are some reasons why diverging from the MVC pattern and why creating a controller inside a view is a bad idea. I'm certain there are some good reasons to do it, but I'll let someone else explore those.

  • I would upvote your answer but I can't, I have been able to test within the view pages and am getting the exact results I required. Other than the issue with readabilty / training. Are there any performance issues, or other issues I may run into. I am not creating a new instance every time the page is loaded, only when the session drops any saved information. That's why it seams pointless to include the model. – 2b77bee6-5445-4c77-b1eb-4df3e5 Aug 26 '14 at 14:14
  • I don't know if you'd experience any performance problems. You mentioned ASCX pages, so I'm assuming your using Microsoft's MVC model as your template. Microsoft wrote the compiler expecting people to use the MVC pattern template properly. It's probable that that the compiler is optimized for follow the MVC pattern as Microsoft has laid it out. I don't know if there would be a performance hit. – Cameron McKay Aug 26 '14 at 14:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.