How to perform 2 different submits in the same action?

Asked

Viewed 31 times

0

I have a view with 2 different forms, when performing the first Submit I receive the data in the controller normally, when performing the second Submit the data comes all as nulls. How can I fix this?

<h2>Selecione o Veículo</h2>
<div class="dropdown">
    <div>
        <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown" id="dropDownMarcas">
            Marcas
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu">
            @{
                try
                {
                    foreach (var item in (List<ConsultaCarrosModel.Marca>)ViewBag.ListaMarcas)
                    {
                        <li><a onclick="addMarca(@item.id,'@item.fipe_name')">@item.name</a></li>
                    }
                }
                catch { }
            }

        </ul>
    </div>
</div>
<form asp-controller="ConsultaCarros" asp-action="Index">
    <div class="form-group" id="formMarca">
        <input type="text" style="display:none" class="form-control" asp-for="marca.id" maxlength="15" placeholder="Código Marca" id="campoMarca">
        <span asp-validation-for="marca.id"></span>
    </div>
    <div class="form-group" id="formMarca1">
        <input type="text" class="form-control" asp-for="marca.name" maxlength="15" placeholder="Marca" id="campoMarca1">
        <span asp-validation-for="marca.name"></span>
    </div>
    <div class="form-group">
        <button type="submit" id="btnMarca" class="btn btn-primary btn-block" style="background-color: green; display:none">EnviarMarca</button>
    </div>
</form>
<div class="dropdown">
    <div>
        <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown" id="dropDownVeiculos">
            Veiculos
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu">
            @{
                try
                {
                    foreach (var item in (List<ConsultaCarrosModel.Veiculos>)ViewBag.ListaVeiculos)
                    {
                        <li><a onclick="addVeiculo(@item.id,'@item.fipe_name')">@item.name</a></li>
                    }
                }
                catch { }
            }


        </ul>
    </div>
</div>
<form asp-controller="ConsultaCarros" asp-action="Index">
    <div class="form-group" id="formVeiculo">
        <input type="text" style="display:none" class="form-control" asp-for="veiculo.id" maxlength="15" placeholder="Codigo Veiculo" id="campoVeiculo">
        <span asp-validation-for="veiculo.id"></span>
    </div>
    <div class="form-group" id="formVeiculo1">
        <input type="text" class="form-control" asp-for="veiculo.name" maxlength="15" placeholder="Veiculo" id="campoVeiculo1">
        <span asp-validation-for="veiculo.name"></span>
    </div>
    <div class="form-group">
        <button type="submit" id="btnVeiculo" class="btn btn-primary btn-block" style="background-color: green; display:none">EnviarVeiculo</button>
    </div>
</form>

<script>
    function addMarca(id, nome) {
        document.getElementById('campoMarca1').value = nome;
        document.getElementById('campoMarca').value = id;
        document.getElementById('btnMarca').click();
    }
    function addVeiculo(id, nome) {
        document.getElementById('campoVeiculo1').value = nome;
        document.getElementById('campoVeiculo').value = id;
        document.getElementById('btnVeiculo').click();
    }
</script>
  • Why not try going through ajax ? Or Voce can create two Forms, and two different action to be able to perform Submit.

  • Post the action code so we can help you better.

No answers

Browser other questions tagged

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