ViewModel property doesn't bind into controller action parameter

945 views Asked by At

My problem is that my ViewModel property doesn't bind into an action parameter. I think it'll be more clear if i just give you my code.

I have a model as follows:

namespace Sima3.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Usuario
    {
        public string Login { get; set; }
        public string NombreCompleto { get; set; }
        public short Organigrama { get; set; }
        public int Interno { get; set; }
        public string EMail { get; set; }
    }
}

And i added DataAnnotations to this partial class in another file (Because this model was generated automatically by EntityFramework), note the remote validation on Login property:

namespace Sima3.Models
{
    [MetadataType(typeof(UsuarioMetaData))]
    public partial class Usuario 
    {
    }
    public class UsuarioMetaData
    {
        [Display(Name = "Nombre de Usuario")]
        [Remote("NoExisteUsuario", "Validation")]
        [Required]
        public string Login { get; set; }
        [Display(Name = "Nombre y Apellido")]
        [Required]
        public string NombreCompleto { get; set; }
        [Display(Name = "Sector")]        
        [Required]
        public short Organigrama { get; set; }
        [Required]
        public int Interno { get; set; }
        [EmailAddress]
        [Required]
        public string EMail { get; set; }
    }
}

Now, i have a ViewModel wich contains a property of type Usuario and some other stuff needed to render my View:

namespace Sima3.ViewModels.PedidoViewModels
{
    public class AgregarViewModel
    {
        public Usuario Usuario { get; set; }
        public Pedido Pedido { get; set; }
        public SelectList ListaSectores { get; set; }
        public SelectList ListaEstados { get; set; }
        public SelectList ListaPrioridades { get; set; }
        public SelectList ListaTipos { get; set; }
        public SelectList ListaDirecciones { get; set; }
    }
}

And my view looks as follows (I'll only post part of it, if u need to see more let me know):

@using (Ajax.BeginForm("Crear", "Usuario", null,
                new AjaxOptions
                {
                    OnSuccess = "UsuarioCreadoSuccess",
                    HttpMethod = "Post"
                }
                , new { @class = "form-horizontal", id = "FormUsuario" }))
            {
                @Html.AntiForgeryToken()
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title title">Nuevo Usuario</h4>
                </div>
                <div class="modal-body">
                    @Html.ValidationSummary(true)
                    <div class="form-group">
                        @Html.LabelFor(model => model.Usuario.Login, new { @class = "col-lg-4 control-label" })
                        <div class="col-lg-7">
                            @Html.TextBoxFor(model => model.Usuario.Login, new { @class = "form-control"})

                            @Html.ValidationMessageFor(model => model.Usuario.Login)
                        </div>
                    </div>

Alright, the important part is the TextBoxFor, it renders me the next HTML:

<input class="form-control valid" data-val="true" data-val-remote="'Nombre de Usuario' is invalid." data-val-remote-additionalfields="*.Login" data-val-remote-url="/Validation/NoExisteUsuario" data-val-required="El campo Nombre de Usuario es obligatorio." id="Usuario_Login" name="Usuario.Login" type="text" value="">

As you see, the textbox name is: name="Usuario.Login"

And my controller action wich gets called by the Remote validation looks like this:

    public JsonResult NoExisteUsuario([Bind(Prefix="Usuario")]string Login)
    {
        Usuario usuario = db.Usuarios.Find(Login);
        if (usuario != null)
        {
            var MensajeDeError = string.Format("El usuario {0} ya existe", Login);
            return Json(MensajeDeError, JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }
    }

I set a breakpoint in this Action and it gets hit, but Login comes in null. I checked with google chrome's debugger the http request header and it shows the form is getting submitted like this: Usuario.Login: asdasdasd.

The question is simple, how can i make it bind?

By the way, i'm using MVC5.

Thanks.

1

There are 1 answers

0
Juanso87 On BEST ANSWER

Well, i finally got it working, it seems i was setting the Prefix binding attribute wrongly. Now my action looks like this:

    public JsonResult NoExisteUsuario([Bind(Prefix="Usuario.Login")]string login)
    {
        Usuario usuario = db.Usuarios.Find(login);
        if (usuario != null)
        {
            var MensajeDeError = string.Format("El usuario {0} ya existe", login);
            return Json(MensajeDeError, JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }
    }