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

Asked

Viewed 243 times

0

I have that 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>

Controller expects a parameter of type string.

I would like to know how I can 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 the value of <select> and send by Html.BeginForms, how can I do this?

  • Can you post the controller code? Or at least the header?

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 for a Controller MostrarDocumentos.

So that’s all you need:

@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 Ubmit onclick="document.forms[0].submit();" specify these reasons in your question.

And the above example will work only if you have the Controller Show documents with request POST with a type parameter string as specified in the commentary.

Example of the Controller that will receive Submit:

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

Browser other questions tagged

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