如何在asp.net mvc视图中使用元组?

时间:2015-08-04 21:04:59

标签: c# asp.net asp.net-mvc asp.net-mvc-4 tuples

我有以下控制器:

 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;
        }

但是我需要在局部视图中能够访问元组然后渲染它。

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

3 个答案:

答案 0 :(得分:5)

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

试试这个:

@foreach (var module in Model.Item2)

答案 1 :(得分:2)

元组没有无参数构造函数,这可能是造成此问题的原因。为什么不创建一个包含字符串和列表并将其传递给视图的视图模型?  参考MVC Custom ViewModel and auto binding

答案 2 :(得分:2)

您可以将@model设置为动态对象,并将其强制转换为Tuple对象。

@model dynamic 

 @{
   var result= model as Tuple<string,List<xx.Models.GlobalAdmin.Models.Modulo>;
 }