How to access controller methods from a view in ASP.NET MVC (jeffreypalermo.com)

submitted by rcashrcash(4149) 9 months, 17 days ago

Suppose my controller has important state or an important function, and I’d like to leverage it from the view. It’s not appropriate to factor out to an html helper or other view-based utility class. It is something unique to the controller. It would be very easy to just pass the controller over to the view so that the view could make use of its methods, but there is a lot of discussion why that is “dirty.”

1 comment |category: |Views: 58

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by vijaystvijayst(1311) 9 months, 16 days ago 0

A good post that describes how to add your own functions within a ViewBag and call it from the View. In this way, a view can call a controller function.

There are two questions that need to be asked. A controller should be lean. Having lot of functions on the controller causes maintenance issue. I prefer to have this function in the business layer - a separate object which the View is aware of. For eg, ViewHelper could be a class in the business layer that could trigger a function IsPrime() on the PrimeNumber object.

For simple implementations like this, it could be an overkill. So, why don't we just have a helper method in the Model itself. I usually do this to do some text parsing.

Reply

information Login or create an account to comment on this story