Two things I learned while trying to solve this problem which are key:
- Razor allows C# which means the .Substring(0,1) method will work
- (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