在部分视图中填充模型,然后将数据返回到部分视图,并在asp.net核心中绑定数据

时间:2018-08-16 06:01:01

标签: asp.net asp.net-mvc asp.net-mvc-partialview asp.net-core-2.1

我仅填写部分视图中需要的一些数据。我在用于部分视图的操作中填充模型,如下所示:

       public PartialViewResult FetchCategoriesForHomePage()
    {
        CommonFunctions.CommonFunctions.APIParameters CategoryListAPiParameters = new CommonFunctions.CommonFunctions.APIParameters
        {
            fields = "*",
            where = "enabled=true",
            count = 1000
        };

        CommonFunctions.CacheHandling objCache = new CommonFunctions.CacheHandling(_cache);

        dynamic categories = CommonFunctions.CommonFunctions.GetAsync(true, (int)(CommonFunctions.CommonFunctions.enumBullHornActionType.query), (int)(CommonFunctions.CommonFunctions.enumBullHornEntity.Category), objCache.GenerateBhRestToken(""), CategoryListAPiParameters).data;

        List<CategoryEntity> listCategories = new List<CategoryEntity>();
        foreach (var item in categories)
        {
            CommonFunctions.CommonFunctions.APIParameters SkillListForcategoryAPIParameters = new CommonFunctions.CommonFunctions.APIParameters
            {
                fields = "*",
                where = "categories.id=" + item.id,
                count = 1000
            };
            List<SkillSet> listSkills = new List<SkillSet>();
            var Skills = CommonFunctions.CommonFunctions.GetAsync(true, (int)(CommonFunctions.CommonFunctions.enumBullHornActionType.query), (int)(CommonFunctions.CommonFunctions.enumBullHornEntity.Skill), objCache.GenerateBhRestToken(""), SkillListForcategoryAPIParameters).data;

            foreach (var skillItem in Skills)
            {
                SkillSet skill = new SkillSet
                {
                    id = skillItem.id,
                    name = skillItem.name
                };
                listSkills.Add(skill);
            }

            CategoryEntity category = new CategoryEntity
            {
                id = item.id,
                name = item.name,
                dateAdded = item.dateAdded,
                Skills = listSkills
            };

            listCategories.Add(category);
        }

        return PartialView(listCategories);
    }

在我看来,代码是:

@model List<Hallis.Entity.CategoryEntity>
<h2>Categories</h2>
<ul>
@foreach (var item in Model)
{
    <li>
        <strong> @item.name :</strong> @foreach (var skill in item.Skills)
        {
            <span>@skill.name,</span>
        }
    </li>
}
</ul>

我已经在另一个视图上调用了它,如下所示:

 @await Component.InvokeAsync("FetchCategoriesForHomePage");

请纠正我我在做什么错。请帮我。

0 个答案:

没有答案