I cannot receive the view parameter in the controller

Asked

Viewed 87 times

0

Remembering that when executing it does not return any error

Controller

using CRUD.Aplicação;
using DocumentoObjeto.dominio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace Tcc1._6._0.Controllers
{
    public class LocalizarController : Controller
    {


        // GET: Localizar

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult RetornoObjetoPorCpf()
        {


            return View();
        }



        [HttpPost]
        public ActionResult RetornoObjetoPorCpf(string cpf)
        {

            ViewBag.cpf = cpf;

            var appAluno = new Inserir_usuario();
            var aluno = appAluno.ListarPorCpf(cpf);

            if (aluno == null)
                return HttpNotFound();

            return View(aluno);
        }




    }
}

View

@model DocumentoObjeto.dominio.Objeto

<div>
    <h4>Objeto</h4>
    <hr />
    <dl class="dl-horizontal" >
        <div class="form-horizontal">


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



            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Buscar" class="btn btn-default" />
                </div>
            </div>
        </div>

        <dt>
            @Html.DisplayNameFor(model => model.nome)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.nome)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.cpf)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.cpf)
        </dd>



    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
    @Html.ActionLink("Back to List", "Index")
</p>

1 answer

1

The submit will send all information inside the form, and note that in your code Submit is outside the form that contains the fields. I advise to put your fields as in the following example:

 @using (Html.BeginForm("RetornoObjetoPorCpf", "LocalizarController", FormMethod.Post))
{
            @Html.LabelFor(model => model.cpf, htmlAttributes: new {@class = "control-label col-md-2"})
            <div class="col-md-10">
                @Html.EditorFor(model => model.cpf, new {htmlAttributes = new {@class = "form-control"}})
                @Html.ValidationMessageFor(model => model.cpf, "", new {@class = "text-danger"})
            </div>

            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Buscar" class="btn btn-default"/>
            </div>
}

  • i put the field inside the form, but msm so when running sql still gets null, since the controller does not receive the view information

  • What type of property cpf in his model?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.