Declare variable within my cshtml

0

I have this code in my cshtml file

<div class="grid_19 box-resultado">
@{
    string
        nm, dia, mes, ano, sexo, numpassaporte,
        diavalidade, mesvalidade,
        anovalidade, paisemissao, dados = "";
.......

I need to declare a variable int , whose value comes from a field in the form. This is the field in my form.

<div class="grid_5">
    <input id="txtTesteQdePass"
           type="text"
           name="txtTesteQdePass"
           class="grid_4  required"
           placeholder="Entre com a qde de passageiros" />
</div>

How do I pass the value of txtTesteQdePass to this variable?

    
asked by anonymous 14.03.2014 / 20:35

3 answers

1

It does not make sense to feed a server side variable at run time without running a postback. Incidentally, this is not even allowed, you would have to perform your server side part to achieve. The best way would be to implement the method in your controller by getting the value of the input, storing the value of the variable in a ViewBag and then making the desired use:

[HttpPost]
public ActionResult Metodo(int txtTesteQdePass)
{
    ViewBag.txtTesteQdePass = txtTesteQdePass;

    return View("nomedaview");
}

Next, in your view, you will have the value stored in @ViewBag.txtTesteQdePass

    
14.03.2014 / 21:40
1

It's not quite the way web systems work.

The text box is something that will appear for the client to populate and then submit to the server ... then yes you will have the opportunity to collect the values that are submitted.

In ASP.NET MVC, you can get the values in a method called action , which falls within a class called Controller . This method can receive the values submitted by the client by a simple name match of the parameters with the IDs of input s, select s and textarea s that are within the submitted form.     

14.03.2014 / 21:44
0

Who will do this data control is your controller. If you want to pass this value to it, just put the name attribute of your input with the variable name that will be received on your backend.

View:

<form method="post" action="/Home/Recebe">
     <input type="text" name="quantidade"/>
     <input type="submit" value="Enviar"/>
</form>

Controller:

public ActionResult(int quantidade)
{
      View(quantidade);
}
    
14.03.2014 / 21:50