Pass value from a select to Html.BeginForms - Asp .Net

0

I have this element <select> :

 <select id="status" class="form-control form-control-sm" name="arquivo" >
            @{ foreach (string valor in ViewBag.ListaArquivos)
                {
                    <option value="@valor">@valor</option>
                }
            }

        </select>

The Controller expects a parameter of type string .

I would like to know how to pass the selected value to this Html.Beginforms :

 @using (Html.BeginForm("MostrarDocumentos", "Usuario", new { arquivo = } ,FormMethod.Post, new { target = "_blank" }))
        {

            <a href="javascript:;" target="_blank" onclick="document.forms[0].submit(); ">Visualizar</a>
            <hr />
        }

I want to get value of <select> and send Html.BeginForms , how can I do this?

    
asked by anonymous 13.12.2018 / 14:05

1 answer

2

Why did you put your selection field <option value="@valor">@valor</option> out of your form ? If you want to send it to the controller it makes sense to leave it on your form.

If I understand correctly, you just want to make a submit of the selected value to Controller MostrarDocumentos .

So you just need it:

@using (Html.BeginForm("MostrarDocumentos", "Usuario", null, FormMethod.Post, new { target = "_blank" }))
{
    <select id="status" class="form-control form-control-sm" name="arquivo">
        @{ foreach (string valor in ViewBag.ListaArquivos)
            {
                <option value="@valor">@valor</option>
            }
        }
    </select>
    <input type="submit" id="btnEnviar" value="Enviar" target="_blank" />
}

If you have any reason to mount your drop down list outside of your form or to make your submission onclick="document.forms[0].submit();" specify those reasons in your question.

And the above example will work only if you have the Controller DisplayDocuments with a strong request with a parameter of type string as specified in the comment.

Controller example that will receive the submit:

[HttpPost]
public ActionResult MostrarDocumentos(string arquivo)
{
    //Código da minha controller que recebe o submit...
    return View();
}
    
14.12.2018 / 12:24