Pass parameter to a C# MVC Controller

Asked

Viewed 7,943 times

3

I am learning programs in ASP.NET MVC C# and would like to know how I pass a data from a textbox to a controller. That is, I want to enter a value(id) in the View Index in a textbox and when I click Submit it returns me to View Details with the id I typed in the Index view. This example is that it takes by the Name, Tel and CPF through the id I type in the view index. The Details view is working perfectly, when I pass the id by url it shows me the person’s data.

Controller

public class HomeController : Controller
{
    //
    // GET: /Home/

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

    public ViewResult Details(int id )
    {
        ClienteEntity cliente = new ClienteEntity(id);
      //  var cli = cliente.FetchUsingPK(id);
        return View(cliente);
    }

}



    {
        ClienteEntity cliente = new ClienteEntity(id);
      //  var cli = cliente.FetchUsingPK(id);
        return View(cliente);
    }

}

Index View

@{
ViewBag.Title = "Index";
}
<h2>Digite um Numero: @Html.TextBox("id")</h2>
<input type="submit" value="Enviar" />

View from the Details

<h2>Dados Do Cliente</h2>
<h2>Nome: @Model.NomeCliente</h2>
<h2>CPF: @Model.Cpf</h2>
<h2>Tel: @Model.Tel</h2>

1 answer

7


Some things were missing:

In his View Index:

@{
    ViewBag.Title = "Index";
}

@using (Html.BeginForm()) 
{
    <h2>Digite um Numero: @Html.TextBox("id")</h2>
    <input type="submit" value="Enviar" />
}

This is because you will send information to a Controller, so it doesn’t make sense to have a TextBox without a form containing it.

In the Controller, add a method that accepts the POST of the information sent, as below:

public class HomeController : Controller
{
    //
    // GET: /Home/

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

    [HttpPost]
    public ActionResult Index(int id)
    {
        return RedirectToAction("Details", new { id = id });
    }

    public ViewResult Details(int id)
    {
        ClienteEntity cliente = new ClienteEntity(id);
      //  var cli = cliente.FetchUsingPK(id);
        return View(cliente);
    }

}


    /* Comentei esta parte porque ela não faz sentido no seu código.
    {
        ClienteEntity cliente = new ClienteEntity(id);
      //  var cli = cliente.FetchUsingPK(id);
        return View(cliente);
    } */

}
  • 2

    Thank you very much even working perfect here.

  • 1

    @Yehudimikhael, if your colleague’s answer has solved your question, you can close his post and don’t forget to give a vote on the answer if you haven’t already done.

  • Can you tell me how I close and vote the answer ?

  • @Yehudimikhael The ideal is for you to take the Tour. Anyway, just below the punctuation, there’s a "V" icon that serves for you to accept the answer. To give votes, just click the arrow above the answer score. Anything just call me.

Browser other questions tagged

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