How to get the Hidden input value inside the controller using Formcollection?

Asked

Viewed 680 times

0

I’m trying to get the value of a Hidden input inside the Controller by Formcollection, but the value is reset. To increase the value, I’m using a Javascript function.

HTML:

  <form id="adicionarTituloManual" action="~/Movimento/AdicionarCodevedoresTit" method="post">
        <input type="hidden" id="qtd" name="qtd" value="" />
    </form>

Javascript:

function novo(i) 
   {
        var form, quant;

        var qtd = document.getElementById('qtd').value

        if (qtd == 0) {
            document.getElementById('qtd').value = i;
        }
        else{
            i =  parseInt(document.getElementById('qtd').value) + 1;
            document.getElementById('qtd').value = i;
        }
    }

Controller:

public ActionResult AdicionarCodevedoresTit(FormCollection formCollection)
{
    if (Session["usuario"] == null)
    {
        return Redirect("~/Home/Index");
    }
    else
    {
        this.usuario = (STLoginUsuario)Session["usuario"];
    }
    int qtd = HUtils.ConverteEmInteiro(formCollection["dvQtd"]);
    return Redirect("~/Movimento/AdicionarCoDevedor");
}
  • The key to the Formcollection should be qtd and not dvQtd. Right?

  • You’re right! Thank you!

1 answer

2


The key to the FormCollection must be the value of the attribute name of input.

The code of controller should be something like

public ActionResult AdicionarCodevedoresTit(FormCollection formCollection)
{
    if (Session["usuario"] == null)
    {
        return Redirect("~/Home/Index");
    }
    else
    {
        this.usuario = (STLoginUsuario)Session["usuario"];
    }

    int qtd = HUtils.ConverteEmInteiro(formCollection["qtd"]);

    return Redirect("~/Movimento/AdicionarCoDevedor");
}
  • if I have an input dynamically created by javascript, how do I get the values in the Controller?

  • I think I’d better open up another question. It’s a completely different subject.

  • By the way, I don’t know if you know, but if the answer helped you, you can mark it as correct using the V on its left side.

  • It’s I tried to dial, but a message popped up asking me to wait 2 minutes! Thanks for reminding

  • But in the case of my question in the comment. The answer is : In the same way!

  • And how will you know on the server side how many fields were created via JS?

Show 1 more comment

Browser other questions tagged

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