简体   繁体   English

.split with razor details C#asp.net core

[英].split with razor details C# asp.net core

Want to take the string NPCSkills and have it split into a list 想要采用字符串NPCSkills并将其拆分为列表

NPCSkills is a prop that includes a string like "guns = 4, armor = 2" I want it to split into a list so I can display it NPCSkills是一种道具,其中包括"guns = 4, armor = 2"类的字符串"guns = 4, armor = 2"我希望将其拆分为一个列表,以便可以显示它

  • item 1 项目1
  • item 2 项目2
  • I am doing the .split but not sure what to do now. 我正在执行.split,但不确定现在要做什么。

     string[] Words = (Model.NPCSkills).Split(',');
     foreach (var item in Words)
     {
         <li>
         @item.Words
         </li>
     }
    

    string does not contain a definition for 'Words' and no extension method 'Words' accepting a first argument of type string could be found (are you missing a using directive or an assembly reference?) + @item.Words string不包含'Words'的定义,也找不到扩展方法'Words'接受string类型的第一个参数(您是否缺少using指令或程序集引用?)+ @ item.Words

    You could use the following code to convert the NPCSkills ( comma-separated string ) to a strongly-typed Dictionary : 您可以使用以下代码将NPCSkills (以comma-separated string )转换为强类型的Dictionary

    var Words = (Model.NPCSkills)
        .Split(',')
        .Select(s => s.Split("="))
        .ToDictionary(arr => arr[0], arr =>arr[1] );
    

    Or if you would like to remove the space around the key/value pair : 或者,如果您想删除键/值对周围的空格,请执行以下操作:

    var Words = (Model.NPCSkills)
        .Split(',')
        .Select(s => s.Split("=").Select(e => e.Trim()).ToArray())
        .ToDictionary(arr => arr[0], arr =>arr[1]);
    

    Now you could render it in a safer way: 现在,您可以以更安全的方式渲染它:

    @foreach(var item in Words)
    {
        <li>
            <span class="badge">@item.Key</span>
            <span class="text">@item.Value</span>
        </li>
    }
    

    暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

    相关问题 C# 和 ASP.NET 核心 6:“会话”中的身份验证和用户详细信息 - C# and ASP.NET Core 6 : authentication and user details in "session" 在ASP.NET Core中具有EF Core的Razor Pages-更新相关数据-使用c#的8之7 - Razor Pages with EF Core in ASP.NET Core - Update Related Data - 7 of 8 with c# 在asp.net核心剃须刀页面应用程序中ajax调用C#函数的“ URL”是什么 - What is the “url” to ajax-call a C# function in an asp.net core razor pages application Asp.net Core Razor 页面无法从 csHTML 获取值到 C# - Asp.net Core Razor Pages Unable to fetch value from csHTML to C# 如何使用 ASP.NET 内核制作 Razor 页面和 C# 代码框架? - How to do you make both a Razor Page and C# code framework with ASP.NET Core? Razor Page C# ASP.NET Core 获取 Windows 用户名(Windows 身份验证) - Razor Page C# ASP.NET Core get Windows Username (Windows Auth) 如何加载网站,然后在 Asp.Net Core (C#) Razor Pages 中添加数据 - How can I load the website and then later add data to it in Asp.Net Core (C#) Razor Pages Asp.Net Core 3.1 Cookies 未附加 Razor 页 C# - Asp.Net Core 3.1 Cookies not appended Razor Pages C# C#ASP.NET Core Razor页面:如何在标记内访问路由约束 - C# ASP.NET Core Razor pages: How to access route constraint within markup PartialViewResult 表单不会清除 ajax 结果上的值 - ASP.NET Core Razor c# - PartialViewResult Form will not clear values on ajax result - ASP.NET Core Razor c#
     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM