Skip to main content
added 64 characters in body
Source Link

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 requested as the string length.

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, 21);
    }

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

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.

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";
    }
    else
    {
        test_add = test.Substring(0, 2);
    }

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

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.

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

Source Link

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.

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";
    }
    else
    {
        test_add = test.Substring(0, 2);
    }

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