Registration with Modal and Partialview . Net Core 2.2

Asked

Viewed 268 times

1

In my project I created a button for registration, which calls a modal that is inside a PartialView. The objective is to register through this modal the data informed in the database, the problem and that when I click on submit modal nothing happens, I’ve researched enough and I haven’t found any explanation of how to do it this way using PartialView.

Controller:

public IActionResult Index()
        {
            var fornecedores = _context.Fornecedores.ToList();
            return View(fornecedores);
        } 

        public IActionResult NovoFornecedor()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> NovoFornecedor (Fornecedores fornecedores)
        {
            if(ModelState.IsValid)
            {
                _context.Add(fornecedores);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(fornecedores);
        }

Index:

 @model IEnumerable<HelpDesk.Models.Fornecedores>

        @{
            ViewData["Title"] = "Cadastro de Fornecedores";
        }


        <div class="container">
            <h4>Fornecedores Cadastrados</h4>
            <p>
                <button class="btn btn-success" data-toggle="modal" data-target="#NovoFornecedor">Novo Fornecedor</button>
            </p>
            <div class="table table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th scope="col">Empresa</th>
                            <th scope="col">CNPJ</th>
                            <th scope="col">Responsavel</th>
                            <th scope="col">Celular</th>
                            <th scope="col">Fixo</th>
                            <th scope="col">Email</th>
                            <th scope="col">Ações</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td>@Html.DisplayFor(modelItem => item.Empresa)</td>
                                <td>@Html.DisplayFor(modelItem => item.CNPJ)</td>
                                <td>@Html.DisplayFor(modelItem => item.Responsavel)</td>
                                <td>@Html.DisplayFor(modelItem => item.Celular)</td>
                                <td>@Html.DisplayFor(modelItem => item.Fixo)</td>
                                <td>@Html.DisplayFor(modelItem => item.Email)</td>
                                <td>
                                      <a asp-action="EditarFornecedor" asp-route-id="@item.FornecedorId" class="btn btn-outline-info btn-sm">Atualizar</a> |
                                <a onclick="ModalExcluir (@item.FornecedorId)" class="btn btn-outline-danger btn-sm">Excluir</a>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>

        @Html.Partial("_NovoFornecedor", new Fornecedores()); 

Partialview containing the registration modal:

@model HelpDesk.Models.Fornecedores

    <div class="modal fade" id="NovoFornecedor">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">Cadastrar Novo Fornecedor</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <form asp-controller="Fornecedores">
              <div class="form-group">
                <label>Empresa:</label>
                <input type="text" class="form-control" asp-for="Empresa">
                <span asp-validation-for="Empresa" class="text-danger"></span>
              </div>
              <div class="form-group">
                <label>CNPJ:</label>
                <input type="text" class="form-control" asp-for="CNPJ">
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">Fechar</button>
            <input type="submit" value="Salvar" class="btn btn-outline-success">
          </div>
        </div>
      </div>
  • 1

    Place the Ubmit button inside the form

  • It worked, I only had to modify the form pointing to Action, thanks for the help.

  • I’ll answer your question not to stay open

  • 1

    ok, just change the modal form to <form Asp-action="Newtemplate"> why if not from the error.

1 answer

1


As a comment, so that the Submit it is necessary that it be inside the form.

It is also necessary in your form indicate the action.

@model HelpDesk.Models.Fornecedores

<div class="modal fade" id="NovoFornecedor">
  <div class="modal-dialog">
    <div class="modal-content">
      <form asp-controller="Fornecedores" asp-action="NovoFornecedor">
        <div class="modal-header">
          <h5 class="modal-title">Cadastrar Novo Fornecedor</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>

        <div class="modal-body">        
          <div class="form-group">
            <label>Empresa:</label>
            <input type="text" class="form-control" asp-for="Empresa">
            <span asp-validation-for="Empresa" class="text-danger"></span>
          </div>
          <div class="form-group">
            <label>CNPJ:</label>
            <input type="text" class="form-control" asp-for="CNPJ">
          </div>        
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">Fechar</button>
          <input type="submit" value="Salvar" class="btn btn-outline-success">
        </div>
      </form>
    </div>
  </div>  
</div>

Browser other questions tagged

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