I have display for my data like
@Html.DisplayFor(m=> m.UserName)
Result
John
I want to show only one character and hide the rest for user name data.
Result that is required
J######
Is there a quick way to display something like this, or will I have to write my own backend logic?
CodePudding user response:
You can try to put @Html.DisplayFor(m=> m.UserName) into div;
and change the content of div with $(function(){}):
<div id="UserName">@Html.DisplayFor(m => m.UserName)</div>
<script>
$(function () {
var userName = document.getElementById('UserName').innerHTML;
var characters = userName.split('');
var newUserName = "";
if (characters.length > 0) {
newUserName = characters[0];
for (var i = 1; i < characters.length; i ) {
newUserName = '#';
}
}
document.getElementById('UserName').innerHTML = newUserName;
})
</script>
CodePudding user response:
What you could do if you won't want to change the model is by using Regex. So instead of @Html.DisplayFor(m=> m.UserName) you would use @Regex.Replace(model.UserName, @"\B[a-z]", "*").
You would have to add @using System.Text.RegularExpressions in the view.
