I make registration on the site for educational purposes. I want to prohibit creating users with the same login. I check for the same login in the interface implementation. Here is the code:

public void Add_New_User(User user) { User olduser = context.Users.FirstOrDefault(c=>c.LoginUser == user.LoginUser); if (olduser == null) { context.Users.Add(user); } else { throw new Exception(); } context.SaveChanges(); } 

That is, if there is no user with such a login, then I register him. If there is a user with such a login, then I push out an error (And I want to change this error to something more beautiful. For example: to the message "There is already a user with this login").

  • The question is what? - Pavel Mayorov
  • @PavelMayorov? Well, look. If a user is registered, and there is already a user in the database with such a login, then his registration must be disabled ... - batya
  • Well, forbid it. The question is what? - Pavel Mayorov

3 answers 3

Sat down. I thought. And I decided that it is better to transfer the verification logic to the controller:

 [HttpPost] public ActionResult Registr(User user, HttpPostedFileBase image) { if (image != null) { user.IconUserType = image.ContentType; user.IconUserData = new byte[image.ContentLength]; image.InputStream.Read(user.IconUserData, 0, image.ContentLength); } User olduser = repository.Users.FirstOrDefault(u => u.LoginUser == user.LoginUser); if (olduser != null) { ModelState.AddModelError("", "Π’Π°ΠΊΠΎΠΉ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ ΡƒΠΆΠ΅ Π΅ΡΡ‚ΡŒ"); } if (user.Password == Request.Form["ΠŸΠΎΠ²Ρ‚ΠΎΡ€ΠΈΡ‚Π΅ ΠΏΠ°Ρ€ΠΎΠ»ΡŒ"] && ModelState.IsValid) { TempData["Message"] = "РСгистрация ΠΏΡ€ΠΎΡˆΠ»Π° ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ! ΠœΠΎΠΆΠ΅Ρ‚Π΅ Π»ΠΎΠ³ΠΈΠ½ΠΈΡ‚ΡŒΡΡ"; repository.Add_New_User(user); return Redirect("/Account/Login"); //Π‘ помошью Ρ‚Π°ΠΊΠΎΠ³ΠΎ ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ΄Π° ΠΌΡ‹ ΠΈΠ·Π±Π΅Π³Π°ΠΌ ошибки, ΠΊΠΎΠ³Π΄Π° ΠΏΡ€ΠΈ ΠΏΠ΅Ρ€Π΅Π³Ρ€ΡƒΠ·ΠΊΠ΅ Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π° ΠΌΡ‹ Π΄Π²Π° Ρ€Π°Π·Π° отправляСм ΠΎΠ΄Π½ΠΈ ΠΈ Ρ‚Π΅ΠΆΠ΅ Π΄Π°Π½Π½Ρ‹Π΅ } else if(user.Password != Request.Form["ΠŸΠΎΠ²Ρ‚ΠΎΡ€ΠΈΡ‚Π΅ ΠΏΠ°Ρ€ΠΎΠ»ΡŒ"]) { ModelState.AddModelError("", "ΠŸΠ°Ρ€ΠΎΠ»ΠΈ Π½Π΅ ΡΠΎΠ²ΠΏΠ°Π΄Π°ΡŽΡ‚"); } return View("Registr"); } 

Is it possible to simplify this somehow, otherwise it seems to me that this controller turned out to be too big ...

    Why do in the method, and not in ActionResult? You can use remote Validation - http://www.c-sharpcorner.com/UploadFile/d87001/remote-validation-in-mvc/

       Public Class RegisterViewModel <Required(ErrorMessage:="НС ΡƒΠΊΠ°Π·Π°Π½ Π»ΠΎΠ³ΠΈΠ½")> <Display(Name:="Π›ΠΎΠ³ΠΈΠ½")> <RegularExpression("[A-z0-9_]{5,15}", ErrorMessage:="ДопустимыС символы - Π»Π°Ρ‚ΠΈΠ½ΠΈΡ†Π°, Π½ΠΈΠΆΠ½Π΅Π΅ ΠΏΠΎΠ΄Ρ‡Π΅Ρ€ΠΊΠΈΠ²Π°Π½ΠΈΠ΅, Ρ†ΠΈΡ„Ρ€Ρ‹ (5-15 символов)")> Public Property Login As String ' имя ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ (Π»ΠΎΠ³ΠΈΠ½) ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Π½Π° ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ End Class 

      check

       If olduser IsNot Nothing Then ModelState.AddModelError("", "ΠŸΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ с Ρ‚Π°ΠΊΠΈΠΌ Π»ΠΎΠ³ΠΈΠ½ΠΎΠΌ ΡƒΠΆΠ΅ сущСствуСт") End If