You can use Partial View of ASP.Net MVC and Jquery Ajax.
In your Controller create a method that returns a Partialview:
public PartialViewResult CheckNickname(string nickname)
{
if (Nicknames.GetNicknames().Exists(x => x == nickname))
ViewBag.StatusNickName = "Nickname existe";
else
ViewBag.StatusNickName = "Nickname não existe";
return PartialView();
}
Right-click on the Checknickname method and click "add view". When you open the View settings window check the box "Create as partial view".

In your Partial View you can put the nickname validation message, I used a Viewbag to return the message (only this code below in partial view):
<h1>@Html.ViewBag.StatusNickName</h1>
Now you need to render Partial View somewhere in your Registration View:
<div id="checkNicknameDiv">
@{
Html.RenderPartial("CheckNickname");
}
This is the Ajax that will call the method that returns the partial view:
<script type="text/javascript">
function checkNickname() {
$.ajax(
{
type: 'GET',
url: '/User/CheckNickname',
data: { nickname: $('#nickname').val() },
dataType: 'html',
cache: false,
async: true,
success: function (data) {
$('#checkNicknameDiv').html(data);
}
});
}</script>
In the parameter data
ajax put the code that will catch the value of the indication field.
To trigger the function checkNickname()
, I created a timer. When starting to type in the field, the timer waits a second without typing to trigger the function, if you continue typing before completing a second, the timer is reset and starts again.
var typingTimer; //timer
var doneTypingInterval = 1000; //tempo ms, 1 segundo por exemplo
var $input = $('#nickname'); //campo de indicação
//no keyup, cria o contador e começa a contagem
$input.on('keyup', function () {
clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
});
//no keydown, elimina o contador
$input.on('keydown', function (e) {
var keyCode = e.keyCode || e.which;
//se teclar tab
if (keyCode == 9) {
checkNickname();
}
else {
clearTimeout(typingTimer);
}
});
//se o tempo acabar, chama a função
function doneTyping() {
checkNickname();
}
Any questions are just talk...!
Filipe thank you so much for your reply, it was exactly what I wanted, it worked correctly and helped me understand the functioning of ASP.NET + Ajax. Note: The only thing is that the timer does not work if you type and TAB to jump line.
– Alisson Marqui
You’re welcome @Alisson! Thanks for the feedback, I changed the javascript to trigger the function if you type tab, check it out. Taking advantage, if everything is right, click on the check to accept the answer...big hug!
– Mr. Mister
Thanks Filipe, only left me one last doubt... Hj my model contains the id_sponsor and in the view the controls are being done on top of an input where the user informs the sponsor nickname. I could put the validation in the Model with Modelstate.Addmodelerror and after validation update in the Model the ID_PATROCINADOR? Thanks in advance for the help and attention.
– Alisson Marqui