简体   繁体   English

将局部视图加载到另一个局部视图中

[英]Load Partial View inside another Partial View

I have a PartialView (_Letra) that receives information from a Controller named Music ... this way 我有一个PartialView(_Letra),它从名为Music的控制器接收信息,这种方式

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). 发生的事情是,现在我需要将其他信息传递到同一PartialView(_Letra)。 This information is in PartialView (_Cifra). 此信息位于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 到目前为止一切正常,PartialView _Cifra接收到该信息

I searched and found that I could use in PartialView _Letra the Html.Partial to load my PartialView _Cifra, I did this way then 我搜索后发现可以在PartialView _Letra中使用Html.Partial加载我的PartialView _Cifra,然后我这样做

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 现在开始复杂化为什么,此返回为null,我相信这是由于我在Html中创建的ResultChords的新实例。

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. 我已经尝试过使用ViewBag在Partials之间转置信息,但是由于返回的也是null,因此可能不正确。

I've already done a lot of research and I'm not getting the information I need for PartialView _Letra. 我已经做了大量研究,但没有获得PartialView _Letra所需的信息。

There is a better way not to use Html.Partial, or to use it properly, as I am not aware. 据我所知,有一种更好的方法是不使用Html.Partial或正确使用它。

In _Letra use 在_Letra中使用

@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 否则,请使用Viewbag并将其设置在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部分的顶部创建它。

_Cifra.cshtml _Cifra.cshtml

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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