views:

21

answers:

1

I use this for checking an existing emailId in my table and inserting it...It works fine how to show message to user when he tries to register with an existing mailId....

if (!taxidb.Registrations.Where(u => u.EmailId == reg.EmailId).Any())
{
     taxidb.Registrations.InsertOnSubmit(reg);
     taxidb.SubmitChanges();
}

and my controller has this,

 RegistrationBO reg = new RegistrationBO();
 reg.UserName = collection["UserName"];
 reg.OrgName = collection["OrgName"];
 reg.Address = collection["Address"];
 reg.EmailId = collection["EmailId"];
 reg.Password = collection["Password"];
 reg.CreatedDate = System.DateTime.Now;
 reg.IsDeleted = Convert.ToByte(0);
 regrep.registerUser(reg);

Any sugesstion how to show "EmailID" already exists to the user with asp.net mvc...

+1  A: 

Make the registerUser repository method return a boolean value indicating whether it has updated the database so that the controller action becomes:

if (!regrep.registerUser(reg))
{
    ViewData["message"] = string.Format("{0} already exists", reg.EmailId);
}

and in your view show the message:

<div><%= Html.Encode(ViewData["message"]) %></div>

If you are using strongly typed view which is recommended then you may add a boolean property to your view model which will indicate whether the database update took place:

model.EmailId = reg.EmailId;
model.IsEmailExists = !regrep.registerUser(reg);
return View(model);

and in the view test the model value:

<% if (Model.IsEmailExists) { %>
    <div><%= Html.Encode(Model.EmailId) %> already exists</div>
<% } %>
Darin Dimitrov