в файле _Layout.cshtml, у меня есть раздел внизу тела под названием "ScriptsContent", объявленный следующим образом:
@RenderSection("ScriptsContent", required: false)
На мой взгляд, я могу использовать этот раздел для добавления скриптов, которые будут выполняться. Но что, если у меня также есть PartialView, который также должен использовать этот раздел для добавления дополнительных скриптов?
Просмотр
@section ScriptsContent
{
<script type="text/javascript">
alert(1);
</script>
}
@Html.Partial("PartialView")
PartialView
@section ScriptsContent
{
<script type="text/javascript">
alert(2);
</script>
}
Результат
Отображается только первый script. Второй script не существует в исходном коде веб-страницы.
Кажется, что Razor выводит только первый скриптовый элемент @section ScriptsContent, который он видит. Я хотел бы знать, есть ли способ объединить каждый вызов в раздел.
Если мы не сможем сделать это, что вы предлагаете?
Благодарю вас за помощь!
UPDATE
Я нашел код, который решает проблему. Посмотрите мой ответ ниже.