Nam Vo
Nam Vo

Reputation: 159

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 src="@(pictureService.GetPictureUrl(productId)"/> ...

Upvotes: 0

Views: 839

Answers (2)

Michael Sagalovich
Michael Sagalovich

Reputation: 2549

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)"/>

Upvotes: 1

Mark Dickinson
Mark Dickinson

Reputation: 6633

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?

Upvotes: 0

Related Questions