TheProgrammer
TheProgrammer

Reputation: 61

Asp dot net MVC Pass Radiobutton Value to Model (or int)

I want to pass the value from my radiobutton to a model, or an integer. The value could be 5 for example.

If I add an input type as text and fill it in, it does pass that value but the value of the radio button will not work.

So my problem is that the id from Adresregelmodel is null

I tried it with an int and with a model but both does not work.

So my code from the view is :

@{
    ViewData["Title"] = "Order";
}
<div class="container">
    <a href="@Url.Action("Index","Winkelwagen")" class="btn btn-primary">Vorige stap</a>

    <h2>Verzending en Betaling</h2>
    @using (Html.BeginForm("Order", "Winkelwagen", FormMethod.Post))
    {
        <label for="des">Adres: </label><br />
        foreach (var adresregel in ViewBag.adresregelViewbag)
        {
            <div class="form-group">
                <label><input type="radio" name="Id" id="@adresregel.Id"> @adresregel.Adres, @adresregel.Postcode, @adresregel.Woonplaats</label>
            </div>
        }
        <input type="text" name="Adres" />
        <input type="submit" class="btn btn-success" value="Bevesting en bestel" />

    }
</div>

This is my controller

        [HttpPost]
        public IActionResult Order(AdresregelModel adresregel, int Id)
        {            
            // Gebruiker email ophalen
            string cookie = Request.Cookies["klantemail"];

            // id gebruiker ophalen
            GebruikerModel gebruikermodel = 
            gebruiker.GetGebruikerByMail(cookie);

            // Weten welke radio button is geselecteerd
            // bestellingmodel maken en vullen

            return View();
        }

This is my Model :

   public class AdresregelModel
   {
       public int Id { get; set; }
       public int GebruikerId { get;  set; }
       public string Adres { get; set; }
       public string Postcode { get; set; }
       public string Woonplaats { get; set; }

   }

Upvotes: 0

Views: 684

Answers (1)

Milney
Milney

Reputation: 6417

You need to set a value="" attribute on your radio button for that value to be passed

Like:

<input type="radio" name="Id" value="@adresregel.Id">

Then the Id property on your model will be populated with whatever @adresregel.Id contains...

You also probably want to take the

, int Id) off your action method:

Upvotes: 1

Related Questions