名称的 Razor 子字符串

发布于 2025-01-08 01:53:00 字数 211 浏览 1 评论 0原文

我需要获取名字的第一个首字母

我有以下内容:

     @Html.DisplayFor(modelItem => item.FirstName.Substring(1,1)) 

但是当程序尝试运行它时,我收到以下错误:

模板只能用于字段访问、属性访问、单维数组索引或单维数组-参数自定义索引器表达式。

I need to get the first initial of the first name

I have the following:

     @Html.DisplayFor(modelItem => item.FirstName.Substring(1,1)) 

I get the following error though when the program tries to run it:

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我太投入 2025-01-15 01:53:00

只需向您的视图模型添加一个属性:

public string FirstName { get; set; }

public string FirstLetterOfName
{
    get
    {
        // TODO: a minimum amount of error checking
        // might be good here for cases when FirstName
        // is null or an empty string
        return this.FirstName.Substring(1, 1);
    }
}

然后:

@Html.DisplayFor(modelItem => item.FirstLetterOfName) 

如果您现在告诉我您没有使用视图模型,而是直接将域实体传递到视图(这是非常糟糕的设计),您可以使用自定义模板:

@Html.DisplayFor(modelItem => item.FirstName, "FirstLetter") 

然后定义一个自定义显示模板 ~/Views/Shared/DisplayTemplates/FirstLetter.cshtml ,其中包含以下内容:

@ViewData.TemplateInfo.FormattedModelValue.Substring(1, 1)

Simply add a property to your view model:

public string FirstName { get; set; }

public string FirstLetterOfName
{
    get
    {
        // TODO: a minimum amount of error checking
        // might be good here for cases when FirstName
        // is null or an empty string
        return this.FirstName.Substring(1, 1);
    }
}

and then:

@Html.DisplayFor(modelItem => item.FirstLetterOfName) 

And if you now tell me that you are not using view models but are directly passing your domain entities to your views (which is very bad design), you might use a custom template:

@Html.DisplayFor(modelItem => item.FirstName, "FirstLetter") 

and then you define a custom display template ~/Views/Shared/DisplayTemplates/FirstLetter.cshtml with the following contents:

@ViewData.TemplateInfo.FormattedModelValue.Substring(1, 1)
黯淡〆 2025-01-15 01:53:00

你不能这样做,因为到你的卑鄙对象的映射不会像这样工作。就像异常消息告诉您只能将其与属性一起使用一样。

但是,您可以编写自己的 HtmlHelper 扩展来执行您想要的操作

public static class HtmlHelpers
{
    public static MvcHtmlString CustomDisplayFor(this HtmlHelper<Person> htmlHelper, Expression<Func<Person, string>> expression)
    {
          var customBuildString = string.Empty;
          //Make your custom implementation here
          return MvcHtmlString.Create(customBuildString);
    }
}

You can't do this, because the mapping to your abject wont work like this. Like the exception message tells you you can only use it with properties.

You can however write your own HtmlHelper extension to do what you want

public static class HtmlHelpers
{
    public static MvcHtmlString CustomDisplayFor(this HtmlHelper<Person> htmlHelper, Expression<Func<Person, string>> expression)
    {
          var customBuildString = string.Empty;
          //Make your custom implementation here
          return MvcHtmlString.Create(customBuildString);
    }
}
笑忘罢 2025-01-15 01:53:00

在substring之前,检查字符串的长度,否则会得到index out of range异常;

@MvcHtmlString.Create(item.title.Length > 20 ? @item.title.ToString().Substring(0, 20):@item.title.ToString())

Before substring, check the length of the string, else you will get index out of range exception;

@MvcHtmlString.Create(item.title.Length > 20 ? @item.title.ToString().Substring(0, 20):@item.title.ToString())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文