I am trying to update a property of an object called Moeda using a drop down list and a save button. But when a press the submit button, the url changes but I'm no getting back to the Index page and nothing is happing to the data
This is my model class:
public class Moeda
{
[Key]
public int Id { get; set; }
[Display(Name = "Tipo de Moeda")]
public string Tipo { get; set; }
public int Quantidade { get; set; }
[DisplayFormat(DataFormatString = "R$ {0:0.00}")]
public float Valor { get; set; }
}
And these are my controller methods:
public IActionResult Add()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Add(String tipo, int quantidade)
{
var moeda = await _context.Moeda
.FirstOrDefaultAsync(m => m.Tipo == tipo);
moeda.Quantidade += quantidade;
_context.Update(moeda);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
And this is my view:
@model MaquinaDeTrocoMVC.Models.Moeda
<!DOCTYPE html>
<html>
<body>
<h1>The select element</h1>
<form action="/moedas/add">
<label for="tipo">Escolha uma moeda</label>
<select name="tipo" id="tipo">
<option value="1 Centavo">1 Centavo</option>
<option value="5 Centavos">5 Centavos</option>
<option value="10 Centavos">10 Centavos</option>
<option value="25 Centavos">25 Centavos</option>
<option value="50 Centavos">50 Centavos</option>
<option value="1 Real">1 Real</option>
</select>
<div class="form-group">
<label asp-for="Quantidade" class="control-label"></label>
<input asp-for="Quantidade" class="form-control" />
<span asp-validation-for="Quantidade" class="text-danger"></span>
</div>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
What am I doing wrong?