Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Check Username Exist or Not using JSON in MVC
WhatsApp
Jithil John
Sep 29
2015
1.7
k
0
1
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
Up Next
Check Username Exist or Not using JSON in MVC