0

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 src="@(pictureService.GetPictureUrl(productId)"/> ...
1
  • Fundamentally, this isnt a great idea. Can you instead do this call through JS / ajax? Commented Jul 6, 2011 at 8:43

2 Answers 2

1

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)"/>
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.