简体   繁体   中英

Load Partial View inside another Partial View

I have a PartialView (_Letra) that receives information from a Controller named Music ... this way

public ActionResult CarregarLetra(string id, string artista, string musica)
{

   return PartialView("_Letra", ArtMus(artista, musica));
}


public ResultLetra ArtMus(string artista, string musica)
{
   //Conteúdo do metodo[..]

   var queryResult = client.Execute<ResultLetra>(request).Data;   

   return queryResult;    
}

Until then, no problem. What happens is that now I need to pass other information to this same PartialView (_Letra). This information is in PartialView (_Cifra).

So I added the following lines in my Music controller

    public ActionResult CarregarCifra(string id, string artista, string musica)
{

   return PartialView("_Cifra", GetCifra(artista, musica));
}


public ResultChords GetCifra(string artista, string musica)
{
      var cfrTest= new Cifra();
      var cifra = new ResultChords();

      cifra.chords = cfrTest.GetInfs(artista, musica);

     return cifra;
}

Everything working so far, PartialView _Cifra receives the information

I searched and found that I could use in PartialView _Letra the Html.Partial to load my PartialView _Cifra, I did this way then

I added

            <div class="item">
            <div class="text-carousel carousel-content">
                <div>
                    @Html.Partial("_Cifra",  new letero.mus.Infra.Models.ResultChords());
                </div>
            </div>
        </div>

Now it starts to complicate why, the return of this is null, I believe it is due to a new instance of ResultChords that I make in Html.Partial

I have already tried using a ViewBag also to transpose the information between Partials, but probably not correctly, due to the return being null as well.

I've already done a lot of research and I'm not getting the information I need for PartialView _Letra.

There is a better way not to use Html.Partial, or to use it properly, as I am not aware.

In _Letra use

@Html.Action("CarregarCifra", "Music", new { id=Model.Id, artista=Model.Artista, musica=Model.Musica });

if the variables are available on the model then you can pass them in; otherwise, make use of the Viewbag and set them in CarregarLetra

Are you always passing a new object to the second partial? You could just create it at the top of the new _Cifra partial.

_Cifra.cshtml

@{
    var resultChords = new letero.mus.Infra.Models.ResultChords();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM