Reputation: 25
Hi im making a shopping cart and i need to register the client, in the database i have two tables one for client and one for users in a one-one relationship in my asp.net mvc i create 2 models one for the client and other for the user, and a clientViewModel how join the two models(client & users) in the view i put a @using(Html.BeginForm) where i request the client & users attributes but when i submit the form to the controller all values are null
This is the ClientViewModel
namespace TiendaRocio.Models
{
public class ClienteViewModel
{
public Cliente Cliente { get; set; }
public Usuarios Usuarios { get; set; }
}
}
This is the view
@model TiendaRocio.Models.ClienteViewModel
@{
ViewBag.Title = "Registrar";
}
<header class="page-header">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header icono">
<a class="navbar-brand" href="~/">
<span class="glyphicon glyphicon-user" aria-hidden="true"> </span>
</a>
</div>
<h1>Registro<small> de usuarios</small></h1>
</div>
</nav>
</header>
@using (Html.BeginForm("Registro", "Ingresar", FormMethod.Post))
{
<div class="form-group">
<label>Nombre</label>
@Html.TextBoxFor(model => model.Cliente.nombre_cliente, new { @class = "form-control", placeholder = "Ingrese aqui su Nombre ", maxlength = 50 })
</div>
<div class="form-group">
<label>Apellidos</label>
@Html.TextBoxFor(model => model.Cliente.apellidos, new { @class = "form-control", placeholder = "Ingrese aqui sus apellidos", maxlength = 50 })
</div>
<div class="form-group">
<label>Dirección</label>
@Html.TextBoxFor(model => model.Cliente.direccion, new { @class = "form-control", placeholder = "Ingrese aqui su Dirección", maxlength = 50 })
</div>
<div class="form-group">
<label>Ciudad</label>
@Html.TextBoxFor(model => model.Cliente.ciudad, new { @class = "form-control", placeholder = "Ciudad", maxlength = 50 })
</div>
<div class="form-group">
<label>Estado</label>
@Html.TextBoxFor(model => model.Cliente.estado, new { @class = "form-control", placeholder = "Estado", maxlength = 50 })
</div>
<div class="form-group">
<label>Codigo Postal</label>
@Html.TextBoxFor(model => model.Cliente.codigo_postal, new { @class = "form-control", placeholder = "Ingrese aqui su Codigo Postal", maxlength = 50 })
</div>
<div class="form-group">
<label>Telefono</label>
@Html.TextBoxFor(model => model.Cliente.telefono, new { @class = "form-control", placeholder = "Ingrese aqui su Telefono", maxlength = 50 })
</div>
<div class="form-group">
<label>Nombre de Usuario</label>
@Html.TextBoxFor(model => model.Usuarios.nombre_usuario, new { @class = "form-control", placeholder = "Ingrese aqui su Nobre de Usuario", maxlength = 50 })
</div>
<div class="form-group">
<label>Contraseña</label>
@Html.TextBoxFor(model => model.Usuarios.contraseña, new { @class = "form-control", placeholder = "Ingrese aqui su contraseña", maxlength = 50 })
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger">Registrar</button>
</div>
}
<div>
@Html.ActionLink("Regresar","Index","Home")
</div>
And this is the controller
public ActionResult Registro(string Nombre_Cliente,string Apellidos,string Direccion,string Ciudad,string Estado,string Codigo_Postal,string Telefono, string Nombre_Usuario, string Contraseña)
{
if (ModelState.IsValid)
{
if (modelo.registrar(new Usuarios { nombre_usuario = Nombre_Usuario, contraseña = Contraseña, permisos = 0 }))
{
if (cm.registrar(new Cliente { nombre_cliente = Nombre_Cliente, apellidos = Apellidos, direccion = Direccion, ciudad = Ciudad, estado = Estado, codigo_postal = Codigo_Postal, telefono = Telefono }))
{
return new RedirectResult("~/Home", false);
}
return new RedirectResult("Registrar", false);
}
else
{
return new RedirectResult("Registrar", false);
}
}
else
{
return new RedirectResult("Registrar", false);
}
}
Upvotes: 2
Views: 1673
Reputation:
Change yor controller method to
[HpptPost]
public ActionResult Registro(ClienteViewModel model)
{
....
}
and the properties of the model will be correctly bound. You generating controls for your model, so post it back!. If you inspect the html your generating you will see elements such as
<input type="text" name="Cliente.nombre_cliente" .../>
not name="nombre_cliente"
so there is no posted value that would match string Nombre_Cliente
Side note: Rather than adding attributes such as maxlength = 50
, use validation attributes (e.g. [StringLength]
on your model properties so that you get both server and client side validation in conjunction with @Html.ValidatinMessageFor()
Upvotes: 3