1

I have a view which will display a username. The username is a property of the object the view is strongly typed to.

my username is formatted as such domain\usernametext

So i want to get the substring of MyModel.username beginning after the last index of \

I tried this but it gives me an invalid operation exception at runtime:

@Html.DisplayFor(x => x.UserName.Substring(x.UserName.LastIndexOf(@"\") + 1))
1
  • 1
    If this is something you're going to do several times or in different places, a Template might be the best choice. Commented Apr 21, 2011 at 14:48

2 Answers 2

2

You can't do this with an HTML Helper.

You can do

@Model.Username.Substring(Model.UserName.LastIndexOf("\") + 1)

to get it to print out. Or you can

@Html.RenderPartial("ViewName", 
   Model.Username.Substring(Model.UserName.LastIndexOf("\") + 1))
Sign up to request clarification or add additional context in comments.

Comments

2
 string str = "AA-22-123456";

 string substr=str.Substring(str.LastIndexOf("-")+1);

Result : 123456

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.