C# @使用(Html.BeginForm)返回空值
您好,我正在制作一个购物车,我需要注册客户机,在数据库中我有两个表,一个用于客户机,一个用于asp.net mvc中的一对一关系中的用户,我创建了两个模型,一个用于客户机,另一个用于用户,以及一个clientViewModel如何在视图中连接两个模型(客户机和用户),我在视图中放置了@using(Html.BeginForm)其中,我请求客户机和用户属性,但当我将表单提交给控制器时,所有值均为nullC# @使用(Html.BeginForm)返回空值,c#,asp.net-mvc,C#,Asp.net Mvc,您好,我正在制作一个购物车,我需要注册客户机,在数据库中我有两个表,一个用于客户机,一个用于asp.net mvc中的一对一关系中的用户,我创建了两个模型,一个用于客户机,另一个用于用户,以及一个clientViewModel如何在视图中连接两个模型(客户机和用户),我在视图中放置了@using(Html.BeginForm)其中,我请求客户机和用户属性,但当我将表单提交给控制器时,所有值均为null This is the ClientViewModel namespace Tiend
This is the ClientViewModel
namespace TiendaRocio.Models
{
public class ClienteViewModel
{
public Cliente Cliente { get; set; }
public Usuarios Usuarios { get; set; }
}
}
这就是观点
@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>
将您的控制器方法更改为
[HpptPost]
public ActionResult Registro(ClienteViewModel model)
{
....
}
并且模型的属性将被正确绑定。您正在为模型生成控件,请将其发回!。如果检查生成的html,您将看到如下元素
<input type="text" name="Cliente.nombre_cliente" .../>
不是name=“nombre\u cliente”
,因此没有与字符串nombre\u cliente匹配的发布值
旁注:不要添加属性,例如maxlength=50
,而是在模型属性上使用验证属性(例如[StringLength]
,这样您就可以结合@Html.ValidatinMessageFor()获得服务器端和客户端验证
您需要使用[HttpPost]
属性标记Registro
方法。
<input type="text" name="Cliente.nombre_cliente" .../>