TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Check Username Exist or Not using JSON in MVC
Jithil John
Sep 29
2015
Code
1.6
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
In the model:
public
class
User
{
[Key]
public
int
UserId {
get
;
set
; }
[System.Web.Mvc.Remote(
"DoesUsernameExist"
,
"Admin"
, HttpMethod =
"POST"
, ErrorMessage =
"Username name already exists."
)]
public
string
Username {
get
;
set
; }
}
In the controller:
[HttpPost]
public
JsonResult DoesUsernameExist(
string
Username)
{
var db =
new
ApplicationDbContext();
var data = db.Users.Where(e => e.Username.Equals(Username, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
return
Json(data ==
null
);
}
DoesUsernameExist
- Action Name
Admin
- Controller Name
We can pass additional fields like this -
[System.Web.Mvc.Remote(
"DoesUsernameExist"
,
"Admin"
, AdditionalFields =
"UserId"
, HttpMethod =
"POST"
, ErrorMessage =
"Username name already exists."
)]
JSON
MVC