ASP点网MVC将单选按钮值传递给模型(或整数)

时间:2019-04-16 12:35:25

标签: c# asp.net-mvc

我想将单选按钮中的值传递给模型或整数。例如,该值可以是5。

如果我将输入类型添加为文本并填写,它会传递该值,但是单选按钮的值将不起作用。

所以我的问题是Adresregelmodel的ID为空

我使用int和模型进行了尝试,但两者均无效。

所以我的视图代码是:

@{
    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>

这是我的控制人

        [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();
        }

这是我的模特:

   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; }

   }

1 个答案:

答案 0 :(得分:1)

您需要在单选按钮上设置一个value =“”属性,以便传递该值

赞:

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

然后,将使用@ adresregel.Id包含的任何内容填充模型中的Id属性...

您可能还想参加

,int ID)关闭操作方法:

相关问题