Declare variable within my cshtml

Asked

Viewed 3,981 times

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 Edit value txtTesteQdePass for this variable?

  • Those lines were missing from the question. <div class="grid_5"> <input id="txtTesteQdePass" type="text" name="txtTesteQdePass" class="grid_4 required" placeholder="Enter passenger qde" /> </div>

  • Did not miss... is because you have to format the question better before sending. You can view the result of what will be sent before sending.

  • @pnet you wanted the field value passed at runtime to the q method you created in cshtml @{ string nm .... } That’s it?

3 answers

1

It makes no sense to feed a server side variable at runtime without running a postback. By the way, this is not even allowed, you would have to run your server side part to get it. The best way would be to implement method in your controller by receiving the input value, storing the variable value in a ViewBag and then making the desired use:

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

    return View("nomedaview");
}

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

1

That’s not exactly how web systems work.

The text box is something that will appear for the client to fill 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 take the values in a method called action, within a class called Controller. This method can receive the values submitted by the client by a simple parameter name match with the Ids of the inputs, selects and textareas within the submitted form.

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 name of the variable that will be received in your backend.

View:

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

Controler:

public ActionResult(int quantidade)
{
      View(quantidade);
}

Browser other questions tagged

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