我有一个视图,其中包含通过部分视图呈现的项目列表。
我希望能够在部分视图中只为整个页面添加一次 javascript 脚本。
我读到我可以用来在所有(部分)视图之间Context.Items
共享数据(如 boolean )。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 视图的字典都是相同的,所以我想每个网页只计算和显示一次。