How can I get substring of object property in ASP.NET MVC View?
I have a view which will display a username. The username is a property of th开发者_StackOverflow社区e 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))
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))
string str = "AA-22-123456";
string substr=str.Substring(str.LastIndexOf("-")+1);
Result : 123456
精彩评论