Send Post with Template data that attributes are classes

Asked

Viewed 53 times

0

I’m trying to send a Post through a form, but in the controller the attributes that are classes are arriving as null, as I should do so I can get the data right so I can register in the database?

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult SalvarPedido(Pedido pedido)
{
    this._dataService.NewPedido(pedido);

    return RedirectToAction("CadPedido", "Manager", new { msg = "Registro salvo com sucesso" });
}

Model ( The User, Vehicle and customer are coming as null )

   public class Pedido
    {
        public int Id { get; set; }
        public Usuario Funcionario { get; set; }
        public Cliente Cliente { get; set; }
        public Veiculo Veiculo { get; set; }
        public DateTime DataRetirada { get; set; }
        public DateTime DataDevolucaoPrevista { get; set; }
        public DateTime DataDevolucao { get; set; }
    }

View

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

@model Pedido

<div class="dark-form">
    <h3 class="title-dark-form">Cadastrar novo pedido</h3>
    <form asp-action="SalvarPedido" asp-controller="Manager" method="post">
        <div class="row form-group">
            <label for="Funcionario" class="col-md-2">Funcionario: </label>
            <div class="col-md-8">
                <select asp-for="Funcionario" class="form-control">
                    @foreach (var funcionario in ViewBag.Usuarios)
                    {
                        <option value="@funcionario.Id">@funcionario.NomeCompleto</option>
                    }
                </select>
            </div>
        </div>
        <div class="row form-group">
            <label for="Cliente" class="col-md-2">Cliente: </label>
            <div class="col-md-8">
                <select asp-for="Cliente" class="form-control">
                    @foreach (var cliente in ViewBag.Clientes)
                    {
                        <option value="@cliente.Id">@cliente.NomeCompleto</option>
                    }
                </select>
            </div>
        </div>
        <div class="row form-group">
            <label for="Veiculo" class="col-md-2">Veículo: </label>
            <div class="col-md-8">
                <select asp-for="Veiculo" class="form-control">
                    @foreach (var veiculo in ViewBag.Veiculos)
                    {
                        <option value="@veiculo.Id">@veiculo.Modelo</option>
                    }
                </select>
            </div>
        </div>
        <div class="row form-group">
            <label for="DataRetirada" class="col-md-2">Data Retirada: </label>
            <div class="col-md-3">
                <input type="date" asp-for="DataRetirada" class="form-control" />
            </div>
            <label for="DataDevolucaoPrevista" class="col-md-2">Data Devolução: </label>
            <div class="col-md-3">
                <input type="date" asp-for="DataDevolucaoPrevista" class="form-control" />
            </div>
        </div> 
        <div class="row form-group">
            <label for="valorPrevisto" class="col-md-2">Valor Previsto: </label>
            <div class="col-md-3" id="valorPrevistro">

            </div>
        </div>
     <div class="row form-group">
            <div class="col-md-10 col-md-offset-2">
                <button type="submit" class="btn btn-primary">Salvar</button>
                <button type="reset" class="btn btn-warning">Limpar</button>
            </div>
        </div>
    </form>
</div>
  • Are all arriving null? Tried to add the [FromBody], SalvarPedido([FromBody]Pedido pedido)

  • I tried, but doing this stops working, goes to a blank page and does not enter the controller

2 answers

0

You have to put the complete path of the model.
Example: @model Datasheet.Models.Request

  • I’ve already added the Models folder to Imports, so I didn’t need to add the full path, that’s not the problem.

0


I found the problem, where I was using Asp-for for fields that are models, I was only putting the name of the model and not the field, I just changed from Client , Vehicle and User to Client.Id, Vehicle.Id and User.Id

Browser other questions tagged

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