我想只显示文件名'Doc1'(减去路径和扩展名)。
我已经尝试了不满意的以下内容,并希望您能分享我做错的任何进一步的亮点......
@functions{
public static string SplitWord(string text, int length)
{
string str = text;
int n = str.LastIndexOf(".");
string str1=str.Substring(n,str.LastIndexOf("/"));
str1=str1.Substring(1,str1.Length);
return str1;
}
}
<ol>
@foreach (var q in AsDynamic(App.Data["CatFilter"]))
{
<li class="sc-element faq-set faq-setOne" data-tags="@String.Join(",", ((List<DynamicEntity>)q.Categories).Select(a => AsDynamic(a).EntityId))">
@q.Toolbar @Edit.Toolbar(actions: "edit,new", contentType: "CatFilter")
<a class="faq-question" style="cursor: pointer">
@if(!String.IsNullOrEmpty(q.LinkText))
{
SplitWord(@q.LinkText,@q.LinkText.Length);
} else {
SplitWord(@q.Link,@q.Link.Length);
}
</a>
</li>
}
</ol>
我也在IF条件下尝试过以下各种变体但又没有运气。
,,,,@:var str = q.Link;
,,,,@:var n = str.lastIndexOf(".");
,,,,@:var str1=str.Substring(n,str.lastIndexOf("/"))
,,,,@:str1=str1.Substring(1,str1.Length);
THX,
答案 0 :(得分:1)
你实际上只是以错误的方式使用Substring。你可能想要
public static string SplitWord(string text, int length)
{
int slash = text.LastIndexOf("/");
int dot = text.LastIndexOf(".");
return text.Substring(slash + 1, dot - slash);
}
尝试一下 - 可能需要在其中一个值上再加上+1或-1,但这应该可以解决问题。
答案 1 :(得分:0)
你是对的(正如你所说的稍微调整一下)....
我还尝试集成日期字段,因此无论用户选择何种日期,它都将始终显示当前日期(上传日期)。
是否有一个字符串可以输入datepicker字段的默认值来显示&#39;今天&#39;而不是用户必须在选择器中单击它? (再次混合问题:))
@functions{
public static string SplitWord(string text,DateTime datetime)
{
int slash = text.LastIndexOf("/");
int dot = text.LastIndexOf(".");
dot--;
var data = text.Substring(slash + 1, dot - slash);
return data + " " + datetime.ToLongDateString();
}
}
@{
var all = AsDynamic(App.Data["CatFilter"]);
}
<ol>
@foreach (var q in AsDynamic(App.Data["CatFilter"]))
{
<li class="sc-element faq-set faq-setOne" data-tags="@String.Join(",", ((List<DynamicEntity>)q.Categories).Select(a => AsDynamic(a).EntityId))">
@q.Toolbar @Edit.Toolbar(actions: "edit,new", contentType: "CatFilter")
<a class="faq-question" style="cursor: pointer">
@if(!String.IsNullOrEmpty(q.LinkText))
{
@q.LinkText
} else {
@SplitWord(q.Link,q.Date);
}
</a>
</li>
}
</ol>
<强>更新强>
这是你的另一个模板,我应用SplitWord函数但导致错误。我看不出上面工作正常的代码有什么不同。
这是我所指的代码:
@using ToSic.SexyContent
@* put all necessary link/script tags here *@
@RenderPage("_Shared - Assets.cshtml", new { parts = "styles,scripts,height"})
@{
// get helper commands
var Helpers = CreateInstance("_Helpers.cshtml");
}
@functions{
public static string SplitWord(string text)
{
int slash = text.LastIndexOf("/");
var data = text.Substring(slash + 1);
return data;
}
}
<div class="clearfix">
<div class="co-container-outer">
<div class="co-container-inner row co-navigation co-navigation-@(Dnn.Module.ModuleID) co-navigation-icon co-navigation-text">
@foreach (var Content in AsDynamic(Data["Default"].List))
{
var linkInfo = Helpers.LinkInfos(Content.Link, Content.Window, Content.Icon);
<div class="col col-xs-12 col-sm-6 col-md-4 sc-element">
@Edit.Toolbar(Content)
@if(linkInfo.Found)
{
@:<a class="co-link-box" title="@Content.Title" href="@Content.Link" target="@linkInfo.Window">
}
<div class="row">
<div class="col-xs-2 text-center">
<i class="co-icon text-primary co-icon fa @linkInfo.Icon" aria-hidden="true"></i>
</div>
<div class="col-xs-10">
<h3>@Content.Link</h3>
<div class="co-ul">
<a class="" title="@Content.Link" href="@Content.SubpageOne">@SplitWord(Content.SubpageOne)</a>
</div>
@if(!String.IsNullOrEmpty(Content.Link))
{
<a href="@Content.Link)"><span class="text-primary">
<!--<i class="glyphicon glyphicon-chevron-right text-primary" aria-hidden="true"></i> -->
@Content.LinkText</span></a>
}
</div>
</div>
@if(linkInfo.Found)
{
@:</a>
}
</div>
}
</div>
</div>
</div>
<script>
/* Call syncHeightResponsive here - makes sure that sync height also works after ajax reload */
$(".co-navigation-@(Dnn.Module.ModuleID) .col").syncHeightResponsive();
</script>