[英]How to split multiple word Models in ASP.NET MVC?
我的项目有名称中包含 2 个或更多单词的模型:
我有一个可以创建面包屑的扩展:
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;
}
来源: https ://stackoverflow.com/a/26439510/153923
但是,我想将名称中包含 2 个或更多单词的项目模型的单词分开。
对于包含多个单词的模型属性,我可以使用[Display(Name = "some thing")]
参数,如下所示:
[Display(Name = "Some Thing")]
public string SomeThing { get; set; }
我尝试将 Display 属性放在类声明中,但 VS2022 说:
属性“显示”在此声明类型上无效。 它仅对“方法、属性、索引器、字段、参数”声明有效。
我做了一些东西并将其放入扩展程序中。 它经历了2次修改,但现在看起来完美无缺。
在这里为其他人添加我的工作:
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;
}
这是调用它两次的 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;
}
它一直对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.