У меня есть представление, в котором содержится список элементов, отображаемых через частичные представления.
Я хотел бы иметь возможность в частичном представлении добавить скрипт javascript только один раз для всей страницы.
Я читал, что могу использовать Context.Items
для обмена данными (например, логическое значение ScriptAlreadyIncluded
) между всеми (частичными) представлениями.
Но могу ли я на это положиться? Или я должен использовать что-то еще?
Мой текущий код (наиболее релевантным частичным представлением является ITypedModel.cshtml и AFieldFormula_DirectFieldFormula.cshtm)
Index.cshtml
@model IEnumerable<MygLogWeb.Classes.MemberField>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
var uid = Guid.NewGuid().ToString();
}
@using (Html.BeginForm("Index", "MemberField", FormMethod.Post, new { id = uid }))
{
@Html.AntiForgeryToken()
<table class="table none">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.IsSystem)
</th>
<th>
@Html.DisplayNameFor(model => model.IsVirtual)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Formula)
</th>
<th></th>
</tr>
</thead>
<tbody>
@Html.EditorFor(model => model)
</tbody>
<tfoot>
<tr>
<td colspan="5">
<input type="button" value="@Resources.Common.Buttons.add" />
</td>
</tr>
</tfoot>
</table>
<input type="submit" value="@Resources.Common.Buttons.save"/>
}
<script type="text/javascript">
new TableEdit("@uid");
</script>
EditorTemplates/MemberField.cshtml
@model MygLogWeb.Classes.MemberField
<tr>
<td>
@Html.DisplayFor(model => model.IsSystem)
@Html.HiddenFor(model => model.IsSystem)
</td>
<td>
@Html.DisplayFor(model => model.IsVirtual)
@Html.HiddenFor(model => model.IsVirtual)
</td>
<td>
@if (Model.IsSystem)
{
@Html.DisplayFor(model => model.Name)
@Html.HiddenFor(model => model.Name)
}
else
{
@Html.TextBoxFor(model => model.Name, new { data_focus = "true" })
@Html.ValidationMessageFor(model => model.Name)
}
</td>
<td>
@Html.EditorFor(model => model.Formula, "ITypedModel")
</td>
<td>
@if (!Model.IsSystem)
{
<input type="button" value="@Resources.Common.Buttons.delete" data-code="delete" />
}
</td>
</tr>
EditorTemplates/ITypedModel.cshtml
...
@foreach (var item in items)
{
<span data-refered-type="@item.Item.Value">
@if (item.Type.IsSubclassOf(typeof(AFieldFormula)))
{
var newModel = item.Item.Selected ? Model : Activator.CreateInstance(item.Type);
@Html.Partial("~/Views/MemberField/EditorTemplates/AFieldFormula_" + item.Type.Name + ".cshtml", newModel)
}
</span>
}
EditorTemplates/AFieldFormula_ConstantFormula.cshtml
@model MygLogWeb.Classes.ConstantFormula
<b>ConstantFormula</b>
@Html.EditorFor(model => model.Constant)
@Html.ValidationMessageFor(model => model.Constant)
EditorTemplates/AFieldFormula_DirectFieldFormula.cshtml
@model MygLogWeb.Classes.DirectFieldFormula
...Build a JSON dictionnary...
<b>DirectFieldFormula</b>
@Html.EditorFor(model => model.Field)
@Html.ValidationMessageFor(model => model.Field)
...Some controls using the JSON dictionnary...
Словарь будет одинаковым для каждого представления AFieldFormula_DirectFieldFormula.cshtml, поэтому я хотел бы вычислить и отобразить его только один раз для каждой веб-страницы.