Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
.NET MVC - Enabling script sections for AJAX-returned partial views

I would like to make use of the (script) section in ASP .NET MVCs, i.e :

@section script
{
    <script>alert("hello");</script>
}

...but with partials views as well, regardless of wether they are used within a complete page or an AJAX-returned content.

It seems like MVC is expecting me to return partial views directly when doing AJAX, by returning a PartialViewResult. But if I do this, the returned view does not have a parent layout, which is where the @RenderSection insctruction is supposed to be, which causes the scripts declared in the script sections not to be rendered.

(By the way it's not a question about whether returning scripts through AJAX is correct, that's another subject)

To fix this issue, I thought about adding the @RenderSection instruction in the partial view. But this would not work, because the partial view is not guaranteed to be the root view of the return content, but also because it would make a duplicated call to @RenderSection, were the partial used in a complete page.

So my second solution is to use an "empty" layout for every AJAX calls that is supposed to return HTML content, with only two instructions in that layout : @RenderBody to render the partial view tree, and @RenderSection to render all the scripts declared in script sections within the partial views.

This second solution seems to perfectly suit my needs. However, it seems like this is not doing AJAX the way MVC is expecting me to, so I am worried this idea causing me problems in the future that I can't foresee at this day.

Would you care to give me your opinion on this ?

Thank you.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda