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)
– Leandro Angelo
I tried, but doing this stops working, goes to a blank page and does not enter the controller
– Leonardo Bressan