@using(Html.BeginForm)返回null值

时间:2015-03-24 04:22:33

标签: c# asp.net-mvc

你好我制作一个购物车,我需要注册客户端,在数据库中我有两个表一个用于客户端和一个用户在一对一的关系在我的asp.net mvc我创建2个模型一个用于客户端和其他用户,以及clientViewModel如何在视图中加入两个模型(客户端和用户)我将@using(Html.BeginForm)放在我请求客户端&用户属性,但是当我将表单提交给控制器时,所有值都为空

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>

这是控制器

    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);
            }
        }

1 个答案:

答案 0 :(得分:3)

将控制器方法更改为

[HpptPost]
public ActionResult Registro(ClienteViewModel model)
{
  ....
}

将正确绑定模型的属性。您为模型生成控件,因此将其发回!如果您检查生成的html,您将看到诸如

之类的元素
<input type="text" name="Cliente.nombre_cliente" .../>

不是name="nombre_cliente"所以没有匹配string Nombre_Cliente

的发布值

旁注:不要添加maxlength = 50等属性,而是在模型属性上使用验证属性(例如[StringLength],以便与@Html.ValidatinMessageFor()一起获得服务器端和客户端验证

相关问题