Recover view value for controller

Asked

Viewed 2,526 times

4

Goal: Retrieve view value for controller [Httppost]

I created a Generic class with gets and sets

public int ListarDadosCarteira_Resultado { get; set; }
public string ListarDadosCarteira_Descricao { get; set; }    
public int ListarDadosCarteira_Total { get; set; }

Updating!! Loading View Complete use two model is only one as I took the second for testing.

    @model Tuple<Generic>
    @{
    ViewBag.Title = "Index";}
    <div class="painel">
    <div class="painel-header">
        <div class="painel-header-text-search">Carteira Mapa</div>
    </div>

    <div class="painel-content">
        @using (Html.BeginForm())
        {  
            <fieldset>
            <legend>Index</legend>


                @Html.TextBoxFor(model => model.Item1.ListarDadosCarteira_Total , new { @class = "newText" })            
                @Html.TextBoxFor(model => model.Item1.ListarDadosCarteira_Descricao , new { @class = "newText" })  
                @Html.TextBoxFor(model => model.Item1.ListarDadosCarteira_Resultado , new { @class = "newText" })  

                <div class="field-row">
                    <div class="editor-label">
                        <div class="posicao-botao">
                                <input type="submit" value="Pesquisar" class="button-search" title="Pesquisar Funcionario Fixa" />
                        </div>
                    </div>
                </div>    
            </fieldset>            
        }
    </div>
</div>
     @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Now comes my doubt how I recover the entered value when I perform a [Httppost] ?

'Cause I usually bring it like this.

[HttpPost]
    public ActionResult Index(Generic generica)
    {
    }

But if I do it this way it’s not bringing me values.

  • Post your complete View here

  • @Randrade Thank you for the first reply, I just updated as requested.

  • You don’t need to use the Tuple. You use the same when you need to pass more than one Model for the view, which is not your case. Your View name is Index also, correct? Only with the Model, without the Tuple didn’t work?

1 answer

5


The idea is the same one you posted, create your model to receive in your Action:

public class Generic
{
    public int ListarDadosCarteira_Resultado { get; set; }
    public string ListarDadosCarteira_Descricao { get; set; }
    public int ListarDadosCarteira_Total { get; set; }
}

Then create your controller with your action:

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult MetodoParaReceberSeuPost(Generic generica)
    {
        return null;
    }
}

And then your view, remembering to type using the @model

@model Mvc4WebBootstrap1.Models.Generic

@using (Html.BeginForm("suaAction", "seuController"))
{ 
    @Html.TextBoxFor(model => model.ListarDadosCarteira_Descricao)
    @Html.TextBoxFor(model => model.ListarDadosCarteira_Resultado)
    @Html.TextBoxFor(model => model.ListarDadosCarteira_Total)

    <input type="submit" value="enviar" />
}

At most that’s it :)

  • Thanks for the first reply. I’m doing just that but it has no value return. I added an add-on to my question maybe it will help to try to locate the problem.

  • @Edgararaujo Just do equal to Brunno’s response. You are passing @model Tuple<Generic>. Pass the model equal to the answer that should solve your problem.

  • @Randrade Worst that I am doing the same and there is no return. I think it is something related to Tuple, because it is the first time I use. has more suggestions ?

  • @Edgararaujo, this one Tuple<Generic> is really strange, try to pass the same in my example and remember to set the action and the controller in @using (Html.BeginForm("suaAction", "seuController")), any I am available :)

Browser other questions tagged

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