SweetPony
SweetPony

Reputation: 19

Access properties of class without creating an instance of the class

I wanna to access the properties of a class without using an instance of the class. My service:

public async Task<MovieDataDetail> GetAllMovieInfo(string title, string lang = "ru")
        {
            var results = await _client.SearchMovieAsync(title, lang);
            var data = results.Results.FirstOrDefault(p => p.Title == title);

            return new MovieDataDetail
            {
                Id = data.Id,
                Title = data.Title,
                OriginalTitle = data.OriginalTitle,
                Overview = data.Overview,
                ReleaseDate = data.ReleaseDate
            };
        }

It writes data to the properties

I would like to use these props on a client but I don't know how to do it without creating the instance of a class. I'd like to do something like that: <div><h1>@MovieDataDetail.Overview</h1></div>

If I create the instance of MovieDataDetail, Overview will be null

Upvotes: 1

Views: 262

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273179

This looks like a simple object management problem.

Instead of creating a (new) instance just assign the result to a variable in your page:

private MovieDataDetail movieDetail = null;

...
   movieDetail = await GetAllMovieInfo(...);

and in the markup section:

@if(movieDetail != null)
{
   <div><h1>@movieDetail.Overview</h1></div>
}

Upvotes: 1

Related Questions