gpt4 book ai didi

c# - 如何在 asp.net mvc View 中使用元组?

转载 作者:太空狗 更新时间:2023-10-30 00:23:58 27 4
gpt4 key购买 nike

我有以下 Controller :

 public class ModuleController : Controller
{
// GET: Modulo
public ActionResult GetModules()
{
return PartialView(Module.GetModulesForUser(ClaimsPrincipal.Current.Identities.First().Name));
}
}

这会返回用户的模块

  /// <summary>
/// Gets the modules activated for a user
/// </summary>
/// <returns>List of modules for the selected user</returns>
public static Tuple<string, List<Modulo>> GetModulesForUser(string identityname)
{
// It needs to be cached for every user because every user can have different modules enabled.
var cachekeyname = "ApplicationModulesPerUser|" + identityname;

var cache = CacheConnectionHelper.Connection.GetDatabase();
Tuple<string, List<Modulo>> modulosPorUsuarioDeDirectorioActivo;

//get object from cache
string modulosUsuariosString = cache.StringGet(cachekeyname);
//string modulosUsuariosString;
if (!string.IsNullOrEmpty(modulosUsuariosString))
{
modulosPorUsuarioDeDirectorioActivo = JsonConvert.DeserializeObject<Tuple<string, List<Modulo>>>(modulosUsuariosString);
return modulosPorUsuarioDeDirectorioActivo;
}

var extPropLookupNameModulos = $"extension_{SettingsHelper.ClientId.Replace("-", "")}_{"Modulos"}";
var client = AuthenticationHelper.GetActiveDirectoryClient();
var user = client.Users.GetByObjectId(identityname).ExecuteAsync().Result;
var userFetcher = (User)user;
var unitOfWork = new UnitOfWork();
var keyvaluepairModulos = userFetcher.GetExtendedProperties().FirstOrDefault(prop => prop.Key == extPropLookupNameModulos);
var idsModulos = keyvaluepairModulos.Value.ToString().Split(',');
var listaModulos= idsModulos.Select(idModulo => unitOfWork.ModuloRepository.GetById(Convert.ToInt32(idModulo))).ToList();
modulosPorUsuarioDeDirectorioActivo = new Tuple<string, List<Modulo>> ( identityname, listaModulos);
//convert object to json string
modulosUsuariosString = JsonConvert.SerializeObject(modulosPorUsuarioDeDirectorioActivo);
//save string in cache
cache.StringSet(cachekeyname, modulosUsuariosString, TimeSpan.FromMinutes(SettingsHelper.CacheModuleNames));
return modulosPorUsuarioDeDirectorioActivo;
}

但是我需要在局部 View 中访问元组然后渲染它。

model Tuple<string,List<xx.Models.GlobalAdmin.Models.Modulo>;

@foreach (var module in Model.Modules)
{
<i class="fa @(@module.ClaseFontAwesome)" title="@module.Nombre"></i>
}

但是我得到这个错误: http://screencast.com/t/lMDg2IYc

最佳答案

您可以通过Item1, ...ItemN 属性访问元组的组件。

试试这个:

@foreach (var module in Model.Item2)

关于c# - 如何在 asp.net mvc View 中使用元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31819307/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com