C# MVC Razor 需要获取子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9398191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:12:40  来源:igfitidea点击:

MVC Razor need to get Substring

c#asp.net-mvcrazor

提问by Nate Pet

I have the following inside of my view

我有以下观点

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

I need to get the first initial of the First Name.

我需要得到名字的第一个首字母。

I tried

我试过

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

but it does not seem to work. I get the following error: .. 'System.Web.Mvc.MvcHtmlString' does not contain a definition for 'Substring' and no extension

但它似乎不起作用。我收到以下错误:.. 'System.Web.Mvc.MvcHtmlString' 不包含 'Substring' 的定义且没有扩展名

采纳答案by Neil Knight

If you are only wanting to display the first character of item.FirstNamewhy not do:

如果您只想显示item.FirstName为什么不这样做的第一个字符:

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

You have it the wrong side of the closing bracket.

你把它放在右括号的错误一侧。

回答by RyanR

You should put a property on your ViewModel for that instead of trying to get it in the view code. The views only responsibility is to display what is given to it by the model, it shouldn't be creating new data from the model.

您应该为此在 ViewModel 上放置一个属性,而不是尝试在视图代码中获取它。视图的唯一职责是显示模型提供给它的内容,它不应该从模型创建新数据。

回答by tvanfosson

Might I suggest that the view is not the right place to do this. You should probably have a separate model property, FirstInitial, that contains the logic. Your view should simply display this.

我是否可以建议该视图不是执行此操作的正确位置。您可能应该有一个单独的模型属性,FirstInitial包含逻辑。您的视图应该简单地显示这一点。

  public class Person
  {
       public string FirstName { get; set; }

       public string FirstInitial
       {
           get { return FirstName != null ? FirstName.Substring(0,1) : ""; }
       }

       ...
   }


   @Html.DisplayFor( modelItem => modelItem.FirstInitial )

回答by Weslley Larentes

You could implement in view as follows:

您可以按如下方式实施:

@Html.DisplayFor(modelItem => modelItem.FirstName).ToString().Substring(0,5)

回答by Murat Y?ld?z

You can use a custom extension method as shown below:

您可以使用自定义扩展方法,如下所示:

/// <summary>
/// Returns only the first n characters of a String.
/// </summary>
/// <param name="str"></param>
/// <param name="start"></param>
/// <param name="maxLength"></param>
/// <returns></returns>
public static string TruncateString(this string str, int start, int maxLength)
{        
    return str.Substring(start, Math.Min(str.Length, maxLength));
}

Hope this helps...

希望这可以帮助...

回答by Marcelo Sitonio

This worked for me (no helper):

这对我有用(没有帮手):

@item.Description.ToString().Substring(0, (item.Description.Length > 10) ? 10 : item.Description.Length )

回答by Andrew Sainsbury

Two things I learned while trying to solve this problem which are key:

我在尝试解决这个问题时学到的两件事是关键:

  1. Razor allows C# which means the .Substring(0,1) method will work
  2. (WHERE I WENT WRONG) - Be very careful that none of your item.FirstNames are empty or contain fewer characters than being requested as the string length.
  1. Razor 允许 C#,这意味着 .Substring(0,1) 方法将起作用
  2. (我哪里出错了) - 非常小心,您的 item.FirstNames 都不是空的,或者包含的字符数少于作为字符串长度请求的字符数。

My solution was the following:

我的解决方案如下:

string test = item.FirstName.ToString();
    string test_add = ""; //creating an empty variable
    if(test.Length == 0) //situation where we have an empty instance
    {
        test_add = "0"; //or whatever you'd like it to be when item.FirstName is empty
    }
    else
    {
        test_add = test.Substring(0, 1);
    }

and you can use @test_add in your razor code in place of @item.FirstName

并且您可以在剃刀代码中使用 @test_add 代替 @item.FirstName