在以前加载的剃刀部分视图中更新模型值
我有一个视图,当视图呈现时加载一组模块(我们将说其他部分视图)。在以前加载的剃刀部分视图中更新模型值
所以认为它像这样在主视图:
<div>
@Html.PartialView("~/Views/MyApp/Partials/Module1.cshtml");
</div>
<div>
@Html.PartialView("~/Views/MyApp/Partials/Module2.cshtml");
</div>
<div>
@Html.PartialView("~/Views/MyApp/Partials/Module3.cshtml");
</div>
有是在局部视图Module2.cshtml
改变的模型值。实际上,它在Module2
视图的操作中发生了变化。我在public ActionResult RenderChecklist(Model pageModel, IGrouping<string, ChecklistItem> list, int checklistCount)
设置模型值:
if (itemCounter == itemCheckedCounter)
{
priorityCounter++;
pageModel.MyAppInfo.ChecklistPriorityLevel = priorityCounter;
listRestrict = "no-restrict";
overlayShow = "hidden";
}
else
{
listRestrict = "no-restrict";
overlayShow = "hidden";
}
根据ChecklistPriorityLevel
值确定在Module1
显示叠加,Module3
等,但由于Module2
之前Module1
负载的ChecklistPriorityLevel
在Module1
的价值始终为0。
发起在被称为每个模块中的局部视图的代码是这样的:
@if (moduleRestrict && !(moduleRestrictPriorityLevel <= checklistPriority) && !Model.GetValue<bool>("moduleRestrictBypass"))
{
const string moduleLockMessage = "This section is locked.";
<div class="module overlay show">
<img src="/assets/myapp/images/lock.png" alt="Module Lock">
<p>@moduleLockMessage</p>
</div>
}
模型相对码只是一个普通的获取,设置在这一刻:
namespace MyApp.Core.MySite.Models
{
/// <summary>
/// Model for MySite.
/// </summary>
public class MyAppInfoModel
{
... //other models
[Ignore]
public int ChecklistPriorityLevel { get; set; }
}
}
所以我的问题是如何获得这一模式的价值的变化来触发其他模块的变化(部分视图)已经加载?
免责声明:我为了隐私目的更改了一些实际的代码。我只是想提供足够的信息让观众了解我想要做的事情。我正在寻找最佳选择,无论是异步还是其他任何方法,都可以正确地获取其他部分视图中的值,而不管首先加载哪些部分。
修复了我自己的问题(但我还是很喜欢看这是怎么处理)
所以对我特别的问题,我决定,我只想逼模型的值设置在加载任何模块之前。我的应用程序的工作方式,Module2可以在任何地方,任何模块可以在Module2之前或之后。在后台确定模块的顺序。所以我决定创建一个SurfaceController并在主视图中获取数据(清单),但是必须在“Module2”中再次获取数据,这是我的ChecklistModule。我不喜欢必须重复检查清单两次,但为了获得这个值,我必须遍历检查清单。
所以我的主要观点通话以下:
MyAppChecklistController.SetChecklistPriorityLevel(Model);
然后我的方法是:
public static void SetChecklistPriorityLevel(MyAppRenderModel pageModel)
{
var checklist = GetCheckList(pageModel);
var priorityCounter = 1;
foreach (var list in checklist)
{
var listCount = list.Count();
var itemData = new ValueTuple<int, int, string>(1, 1, null);
itemData = list.OrderBy(x => x.IsChecked).ThenBy(x => x.SortOrder).Aggregate(itemData,
(current, item) => GetItemList(item, current.Item1, current.Item2, current.Item3, listCount));
var itemCounter = itemData.Item1;
var itemCheckedCounter = itemData.Item2;
// var itemList = itemData.Item3;
priorityCounter = GetPriorityLevel(itemCounter, itemCheckedCounter, priorityCounter);
pageModel.ChecklistPriorityLevel = priorityCounter;
}
}
然后,当我呈现在ChecklistModule局部视图的清单:
[ChildActionOnly]
public ActionResult RenderChecklist(IGrouping<string, ChecklistItem> list,
int checklistCount, int checklistCounter, int priorityCounter)
{
var parentFolder = list.First().UmbracoContent.Parent;
var listCount = list.Count();
var itemData = new ValueTuple<int, int, string>(1, 1, null);
var color = GetChecklistColorValue(parentFolder);
itemData = list.OrderBy(x => x.IsChecked).ThenBy(x => x.SortOrder).Aggregate(itemData,
(current, item) => GetItemList(item, current.Item1, current.Item2, current.Item3, listCount));
var itemCounter = itemData.Item1;
var itemCheckedCounter = itemData.Item2;
var itemList = itemData.Item3;
var checklistDict = GetChecklistRestrictions(parentFolder, itemCounter,
itemCheckedCounter, priorityCounter);
checklistDict.Add("color", color);
checklistDict.Add("itemList", itemList);
checklistDict.Add("checklistCounter", checklistCounter);
return GetChecklistLayout(checklistCount, checklistLayoutDict);
}
所以你可以看到我几乎在一个页面加载中运行两次检查清单。我不想这样做。如果任何人有更好的想法让这个更高效,让我知道。
UPDATE:上述解决方案(即使只有部分代码张贴)固定我的问题,但我决定,我会正好赶上该清单上的观点,并把它添加到型号太多,然后使用模型(pageModel.Checklists)在部分,所以我实际上并没有用Linq两次抓取清单。我仍然需要两次检查清单,但是我没有两次抓取这些值。所以更多的是黑客攻击,但也许我会在稍后继续寻找方法来精简这一点。