1

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?

2 Answers 2

2

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));
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply @Serge. I followed your suggestion. The action is generating a new Moeda to the list, rather than adding to the existing Moeda. How can I proceed?
@WillI'm Use my code and let me know if you have any problem with this
I tried using it this way: link But I think It's not right. link
@WillI'm You are using an old .net , I don' t know what is going to work using this version
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.