Send parameters from the view to the controller in C #

0

At the moment of receiving the parameters in the Controller from the View, the same arrive null, in the View I have the following code:

@model Syc.Visitantes.Dominio.Entidades.Usuario

@{
    ViewBag.Title = "Crear Usuario";
}

<h2>Crear Usuario</h2>

<div>
    @Html.ActionLink("Volver", "Index")
</div>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        <label class="control-label col-md-2">Nombre</label>
        <div class="col-md-10">
            @Html.EditorFor(model => Model.Nombre, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => Model.Nombre, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2">Correo</label>
        <div class="col-md-10">
            @Html.EditorFor(model => Model.Correo, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => Model.Correo, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2">Código de Acceso</label>
        <div class="col-md-10">
            @Html.EditorFor(model => Model.CodigoAcceso, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => Model.CodigoAcceso, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2">Clave de Acceso</label>
        <div class="col-md-10">
            @Html.EditorFor(model => Model.ClaveAcceso, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => Model.ClaveAcceso, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2">Rol</label>
        <div class="col-md-10">
            @Html.DropDownList("RolId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => Model.RolId, "", new { @class = "text-danger" })    
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">            
            @Html.ActionLink("Crear", "Crear", "Usuarios")
        </div>
    </div>
</div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

And this is the controller code:

    using Syc.Visitantes.Aplicacion.Controladores;
    using Syc.Visitantes.Dominio.Entidades;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;

    namespace Visitantes.Controllers
    {
        public class UsuariosController : Controller
        {
            public ActionResult Crear([Bind(Include = "Nombre,Correo,CodigoAcceso,ClaveAcceso,RolId")] Usuario usuario)
            {
                if (ModelState.IsValid)
                {
                    UsuarioControlador controlador = new UsuarioControlador();
                    Usuario usuarioCreador = new Usuario();
                    controlador.Crear(usuario);
                    return RedirectToAction("Index");
                }
                return View(usuario);
            }
    }
}

But when verifying I realize that user receives the null values from the view.

    
asked by Alberto Atencio 23.04.2018 в 17:11
source

1 answer

3

The first thing I can comment on is that you do not use the line

@Html.EditorFor(model => Model.Nombre, ...

Model with the capital M, remember that c # is key-sensitive, so no references to model of the lambda, use

@Html.EditorFor(model => model.Nombre, ...

model with minuscule

On the other hand the @Html.ActionLink does not make a post to the action, you should define a submit button to send the data.

Basically you have two action, one that is GET to load the view when you enter and one POST where you receive the submit of the form

public class UsuariosController : Controller
{

    public ActionResult Crear()
    {
        //aqui instancia un usuario por defecto para el model
        Usuario usuario = new Usuario();
        return View(usuario);
    }

    [HttpPost]
    public ActionResult Crear(Usuario usuario)
    {
        if (ModelState.IsValid)
        {
            UsuarioControlador controlador = new UsuarioControlador();
            Usuario usuarioCreador = new Usuario();
            controlador.Crear(usuario);
            return RedirectToAction("Index");
        }
        return View(usuario);
    }


}

But to receive the post you need a button of type="submit" and not an action link

    
answered by 23.04.2018 в 18:02