Home > other >  How update a property of an object using a drop-down list in ASP.NET MVC?
How update a property of an object using a drop-down list in ASP.NET MVC?

Time:01-18

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 >
            <label asp-for="Quantidade" ></label>
            <input asp-for="Quantidade"  />
            <span asp-validation-for="Quantidade" ></span>
        </div>
        <br><br>
        <input type="submit" value="Submit">
    </form>    

</body>
</html>

What am I doing wrong?

CodePudding user response:

you have to add asp-for to select and @Html.AntiForgeryToken() to form

@{
    var items = new List<SelectListItem>
    {
        new SelectListItem { Value="1 Centavo",   Text="1 Centavo" },
        new SelectListItem { Value="5 Centavos",  Text="5 Centavos" },
        new SelectListItem { Value="10 Centavos", Text="10 Centavos" },
        new SelectListItem { Value="25 Centavos", Text="25 Centavos" },
        new SelectListItem { Value="50 Centavos", Text="50 Centavos" },
        new SelectListItem { Value="1 Real",      Text="1 Real" }
    };
}

@using (Html.BeginForm("add", "moedas", FormMethod.Post))
{
 @Html.AntiForgeryToken() 
....
 <select asp-for="Tipo"  asp-items="@items" id="tipo"> select </select>
.....

}

and fix the actions

public IActionResult Add()
{
var model= new Moeda();
    return View(model);
}

public async Task<IActionResult> Add(Moeda moeda)
{
     _context.Moeda.Add(moeda);
 
    await _context.SaveChangesAsync();

    return RedirectToAction(nameof(Index));
}

CodePudding user response:

Update your list first with the newly added item in the Add(..) method then update list in the model and finally return view with updated model. If you have correctly mapped your list from backed to the view drop down list then this should be working.

  •  Tags:  
  • Related