HTML and CSS Reference
In-Depth Information
[HttpPost]
public ActionResult Index(User user)
{
if (ModelState.IsValid)
{
UserDbEntities db = new UserDbEntities();
db.Users.AddObject(user);
db.SaveChanges();
return View("Success");
}
else
{
return View("Index", user);
}
}
The
Index()
action methods are straightforward. The second
Index()
action method accepts a
User
instance and adds it to the database. Notice the use of the
ModelState.IsValid
property, which returns
true
only if the data model (
User
object) meets all the validation criteria. If user registration is successful,
the Success view is rendered. Otherwise Index view is displayed with validation errors.
The
User
controller also contains two action methods that are invoked from jQuery code. These
methods check for duplicate display names and e-mail addresses and return
true
if a duplicate is found.
Listing 5-28 shows these action methods.
Listing 5-28.
Checking Duplicate Display Names and E-mail Addresses
[HttpPost]
public JsonResult IsDuplicateEmail(string email)
{
UserDbEntities db = new UserDbEntities();
var data = from item in db.Users
where item.Email == email
select item;
bool lag=false;
if (data.Count() > 0)
{
flag = true;
}
return Json(flag);
}
[HttpPost]
public JsonResult IsDuplicateDisplayName(string displayname)
{
UserDbEntities db = new UserDbEntities();
var data = from item in db.Users
where item.DisplayName == displayname
select item;
bool flag = false;
if (data.Count() > 0)
{