asp.net MVC View page call server side methods
I'd like to know how to call server side methods from a view page. I need a mean to call directly server side methods whatever I need(not just from a model passed from controller)
<img s开发者_开发技巧rc="@(pictureService.GetPictureUrl(productId)"/> ...
You will need to pass pictureService
from a controller via ViewData or via Model, e.g., extend your Model type to contain that property, or use dynamic models add dynamically add such property in controller.
You could possibly also create static class PictureService
and call it like you do ni the sample:
<img src="@(MyNamespace.PictureService.GetPictureUrl(productId)"/>
or
@using MyNamespace;
...
<img src="@(PictureService.GetPictureUrl(productId)"/>
What about using en EmptyResult, or a RedirectResult. You don't have to return something, but doing this lets you run a method server side. You could also use a JSON result, and call it using jQuery, then you could return a boolean to say whether it worked or not.
It's hard to tell from your example code, why you don't just serve the image urls with the page. If your image src is known when you serve the page, why not include it in your view model?
精彩评论