简体   繁体   English

如何在 ASP.NET MVC 中拆分多个单词模型?

[英]How to split multiple word Models in ASP.NET MVC?

My project has models with 2 or more words in the name:我的项目有名称中包含 2 个或更多单词的模型:

  • EngineConfigurationModel引擎配置模型
  • MyProductModel我的产品模型
  • CurrentProductModel当前产品型号
  • CheckNetworkInventoryModel检查网络库存模型

I've got an extension that can create a breadcrumb:我有一个可以创建面包屑的扩展:

public static string BuildBreadcrumbNavigation(this HtmlHelper helper)
{
    // optional condition: I didn't wanted it to show on home and account controller
    if (helper.ViewContext.RouteData.Values["controller"].ToString() == "Home" ||
        helper.ViewContext.RouteData.Values["controller"].ToString() == "Account")
    {
        return string.Empty;
    }

    var htmlLink = helper.ActionLink("Home", "Index", "Home").ToHtmlString();
    var sb = new StringBuilder("<ol class='breadcrumb'><li>");
    sb.Append(htmlLink);
    sb.Append("</li>");
    sb.Append("<li>");
    sb.Append(helper.ActionLink(helper.ViewContext.RouteData.Values["controller"].ToString().Titleize(),
                                        "", // "Index",
                                        helper.ViewContext.RouteData.Values["controller"].ToString()));
    sb.Append("</li>");

    if (helper.ViewContext.RouteData.Values["action"].ToString() != "Index")
    {
        sb.Append("<li>");
        sb.Append(helper.ActionLink(helper.ViewContext.RouteData.Values["action"].ToString().Titleize(),
                                            helper.ViewContext.RouteData.Values["action"].ToString(),
                                            helper.ViewContext.RouteData.Values["controller"].ToString()));
        sb.Append("</li>");
    }
    var result = sb.Append("</ol>").ToString().Replace("Index", "");
    return result;
}

Source: https://stackoverflow.com/a/26439510/153923来源: https ://stackoverflow.com/a/26439510/153923

But, I want to split-up the words for project models with 2 or more words in the name.但是,我想将名称中包含 2 个或更多单词的项目模型的单词分开。

  • for EngineConfigurationModel, class name EngineConfiguration would be 'Engine Configuration'对于 EngineConfigurationModel,类名 EngineConfiguration 将是“Engine Configuration”
  • MyProductModel, class name MyProduct would be 'My Product' MyProductModel,类名 MyProduct 将是“我的产品”
  • CurrentProductModel, class name CurrentProduct would be 'Current Product' CurrentProductModel,类名 CurrentProduct 将是“当前产品”
  • CheckNetworkInventoryModel, class name CheckNetworkInventory would be 'Check Network Inventory' CheckNetworkInventoryModel,类名 CheckNetworkInventory 将是“检查网络库存”

For model properties with multiple words, I can use a [Display(Name = "some thing")] parameter like this:对于包含多个单词的模型属性,我可以使用[Display(Name = "some thing")]参数,如下所示:

[Display(Name = "Some Thing")]
public string SomeThing { get; set; }

I tried putting the Display attribute on the class declaration, but VS2022 says:我尝试将 Display 属性放在类声明中,但 VS2022 说:

Attribute 'Display' is not valid on this declaration type.属性“显示”在此声明类型上无效。 It is only valid on 'method, property, indexer, field, parameter' declarations.它仅对“方法、属性、索引器、字段、参数”声明有效。

CS0592

I made something and I put it into an extension.我做了一些东西并将其放入扩展程序中。 It has gone through 2 revisions, but it seems to be flawless now.它经历了2次修改,但现在看起来完美无缺。

Adding my work here for others:在这里为其他人添加我的工作:

public static string SplitTitleWords(this string value)
{
    var cList = new List<char>();
    if (!string.IsNullOrEmpty(value))
    {
        cList.Add(value[0]); // just add the first letter, whether caps, no caps, or number
        for (var i = 1; i < value.Length; i++)
        {
            var c = value[i];
            if (char.IsUpper(c))
            {   //                                01234567891234    0123456789012345  
                // check special cases like class AddPDFResource => Add PDF Resource
                var c0 = value[i - 1];
                if (char.IsUpper(c0))
                {
                    if (i + 1 < value.Length)
                    {
                        var c1 = value[i + 1];
                        if (!char.IsUpper(c1))
                        {
                            cList.Add(' ');
                        }
                    }
                } else
                {
                    cList.Add(' ');
                }
            }
            cList.Add(c);
        }
    }
    var result = new String(cList.ToArray());
    return result;
}

And here is a Breadcrumb extension method that calls it twice:这是调用它两次的 Breadcrumb 扩展方法:

public static string BuildBreadcrumbNavigation(this HtmlHelper helper)
{
    var result = string.Empty;
    var controllerName = helper.ViewContext.RouteData.Values["controller"].ToString();
    // optional condition: I didn't wanted it to show on home and account controller
    if ((controllerName != "Home") && (controllerName != "Account"))
    {
        var homeLink = helper.ActionLink(
            linkText: "Home",
            actionName: "Index",
            controllerName: "Home").ToHtmlString();
        var sb = new StringBuilder($"<ol class='breadcrumb'><li>{homeLink}</li>");

        var url = HttpContext.Current.Request.Url.ToString();
        var urlParts = url.Split(new char[] { '/' });

        if (!urlParts.Contains("Console"))
        {
            var controllerLink = helper.ActionLink(
                linkText: controllerName.SplitTitleWords(),
                actionName: "Index",
                controllerName: controllerName);
            sb.Append($"<li>{controllerLink}</li>");
        } else
        {
            var a = $"<a href=\"{url}\">Console</a>";
            sb.Append($"<li>{a}</li>");
        }

        var actionName = helper.ViewContext.RouteData.Values["action"].ToString();
        sb.Append($"<li class=\"active\">{actionName.SplitTitleWords()}</li>");

        result = sb.Append("</ol>").ToString();
    }
    return result;
}

It has been working great for me.它一直对我有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM