ASP.NET MVC: Access controller instance from view

后端 未结 2 1876
北海茫月
北海茫月 2020-12-25 10:56

How can I access a controller instance from the view? E.g. I have a HomeController which then returns my Index view. Inside of that view I want to

2条回答
  •  温柔的废话
    2020-12-25 11:48

    In my opinion, you should consider a design where the View doesn't need to know about the Controller. The idea is that the Controller deals with the request, conjures up a Model and hands that Model off to the View. At that point, the Controller's work is done.

    I think it is an indication of a design flaw if the View needs to know anything about the Controller. Can you share more about what it is that you are trying to accomplish?

    I often find that when dealing with well-designed frameworks (such as the MVC framework), if it feels like the framework is fighting you, you are probably going about the task in the wrong way. This has happened to me a lot, and stepping back and asking myself what it is that I'm really trying to accomplish often leads to new insights.

提交回复
热议问题