Post Post with Model data that attributes are classes

0

I'm trying to send a Post through a form, but in the controller the attributes that are classes are coming in as null, how should I do it so I can get the data right so I can register with the bank?

Controller

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

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

Template (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>
    
asked by anonymous 09.02.2018 / 12:47

2 answers

1

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

    
21.02.2018 / 02:11
0

You have to put the complete path of the model. Home Example: @model ProjectName.Models.Please

    
12.02.2018 / 16:01