How to access a private static array from another class?

-4
private static List<Receta> _recetas = new List<Receta>
{
    //contenido array
}; 
public List<Receta> GetRecetas()
{
    return _recetas;
}

This is the code if I have a Get method but how do I access it from another class without passing null ?, sorry for asking so much, I've really been trying to do two models in a view for days ...

The full code can be found at GitHub

    
asked by CeciPeque 12.07.2018 в 22:48
source

2 answers

1

Since I have no idea what you are trying to do, I am sending you a mincode that accesses a static private list:

public class Receta{
    public string ingrediente;
}

public class libro{
    private static List<Receta> _recetas = new List<Receta>();
    public List<Receta> GetRecetas(){
        return _recetas;
    }
}

What I do not understand is how you try to make the public list<recetas> getrecetas() method inside of _recetas ... that can not be ....

Now let's imagine that this is your main :

libro libroRecetas = new libro();
libroRecetas.GetRecetas().Add(new Receta { ingrediente="tocino" });
Console.WriteLine(libroRecetas.GetRecetas()[0].ingrediente);
Console.ReadLine();

Try to see what you are doing wrong, obviously if your list is empty it will not send you any results.

    
answered by 12.07.2018 в 23:10
1

Well I just took a look at your repository and as I can see two errors that do not allow you to load the page correctly. Of course you have to give another turn to what is the MVC pattern but this will allow you to at least execute it:

On the one hand in your class HomeController :

public ActionResult Index()
    {
        //var viewModel = Modelo.RepositorioRecetas.GetRecetas(); Código antiguo
        var viewModel = new Modelos();

        return View(viewModel);
    }

On the other hand, in the class Modelos add a constructor that initializes the necessary, for example:

 public Modelos()
    {
        RepositorioRecetas = new RepositorioRecetas();
    }

If then on the main page in the body of Index.cshtml you make the following change:

    @if (Model.RepositorioRecetas != null)
        {
        <div class="row">
            @* Linea que tenias anteriormente y he comentado *@
            @*@foreach (Receta receta in Model.RepositorioRecetas)*@ 
                @foreach (Receta receta in Model.RepositorioRecetas.GetRecetas())
                {
                    <div class="col-md-4">
                        <div class="well">
                            <h4>@Html.ActionLink(receta.Titulo, "Receta", new { id = receta.Id, @class = "Recetas" })</h4>
                            <a href="@Url.Action("Receta", new {id = receta.Id})">
                                <img src="~/img/fotos/@receta.CoverImageFileName" alt="@receta.MostrarTexto" class="img-responsive" />
                            </a>
                        </div>
                    </div>
                }
        </div>
    }

With this you already load at least the main page. A lot of encouragement and patience

    
answered by 13.07.2018 в 08:20