I'm trying to do a login and register view with c#, html and MVC. First of all, I have a table in my database called "Usuarios" with columns like: Username, Contrasena (Password), Email, etc). When i fill all the text boxes to test if my controller update my database (adding the new user) it doesnt work.
My RegisterClient() method in ClientController:
[HttpPost] [AllowAnonymous] public ActionResult RegisterClient(RegisterViewModel model) {if(ModelState.IsValid) { Cliente Client = new Cliente(model.NombreUsuario, model.Password, model.Nombre, model.Apellido, model.Domicilio, model.TipoDoc, model.NumeroDoc, model.Telefono); using (var db = new ContextoDB()) { if (db.Usuarios.Any(u => u.NombreUsuario == Client.NombreUsuario)) { ViewBag.DuplicateMessage = "Nombre de usuario en uso."; return View("RegisterClient", model); } db.Usuarios.Add(Client); db.SaveChanges(); } ModelState.Clear(); ViewBag.SuccessMessage = "Registado excitosamente!"; return RedirectToAction("RegistrationSuccess", "Account"); } else { return View(model); } }
The RegisterClient View:
<div class=“register-box”>
<h4 class=“h4-header”>Registro paso 2 de 2 </h4>@Html.ValidationSummary(true, "Register failed. Check your login details.") <fieldset class="fieldset-box client-field"> <div class="user-info-box"> <h3> Datos de la cuenta</h3> <div class="editor-field"> @Html.TextBoxFor(model => model.NombreUsuario, new { Class = "YourBackgroundClass", Placeholder = "Usuario" }) @Html.ValidationMessageFor(model => model.NombreUsuario, "", new { @class = "text-danger" }) </div> <div class="editor-field"> @Html.TextBoxFor(model => model.Email, new { Class = "YourBackgroundClass", Placeholder = "Email" }) @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) </div> <div class="editor-field"> @Html.PasswordFor(model => model.Password, new { Class = "YourBackgroundClass", Placeholder = "Contraseña" }) @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" }) </div> <div class="editor-field"> @Html.PasswordFor(model => model.ConfirmPassword, new { @class = "YourBackgroundClass", Placeholder = "Confirmar contraseña" }) @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" }) </div> </div> <div class="personal-info-box"> <h3> Datos personales</h3> <div class="editor-field"> @Html.TextBoxFor(model => model.Nombre, new { Class = "YourBackgroundClass", Placeholder = "Nombre" }) </div> <div class="editor-field"> @Html.TextBoxFor(model => model.Apellido, new { Class = "YourBackgroundClass", Placeholder = "Apellido" }) </div> <div class="editor-field"> @Html.TextBoxFor(model => model.Domicilio, new { Class = "YourBackgroundClass", Placeholder = "Domicilio" }) </div> <div class="editor-field"> @Html.TextBoxFor(model => model.Telefono, new { Class = "YourBackgroundClass", Placeholder = "Telefono" }) </div> <div class="editor-field"> @Html.TextBoxFor(model => model.NumeroDoc, new { Class = "YourBackgroundClass", Placeholder = "Numero de documento" }) </div> </div> </fieldset> <form action="" method="post"> <input type="submit" class="but-next next round" name="upvote" value="&#8250;" /> </form> <a href="#" class="but-next next round">&#8250;</a> </div>
When i fill all the boxes, it triggers the validation messages.
#c-sharp #html