简体   繁体   English

将单选按钮绑定到用户输入ASP.NET MVC Razor

[英]Bind Radio button to user input ASP.NET MVC Razor

I have a view in which users enter their primary phone number and then their mobile number and selects a radio button on which number he prefers to be contacted on. 我有一个视图,用户在其中输入他们的主要电话号码,然后输入他们的手机号码,并选择一个单选按钮,在该单选按钮上他希望与之联系。

this is my code.... 这是我的代码。

 /*<div class="form-group col-md-3">
    @Html.Label("First Name")
    @Html.TextBoxFor(m => m.FirstName)
 </div>
 <div class="form-group col-md-3">
     @Html.Label("Last Name")
     @Html.TextBoxFor(m => m.LastName)
 </div>*/
 <div>
      @Html.Label("Primary Number")
      @Html.TextBoxFor(m => m.PrimaryNumber)
      @Html.RadioButtonFor(m => m.Contact, null)
 </div>
 <div>
      @Html.LabelFor(m => m.Mobile)
      @Html.TextBoxFor(m => m.Mobile)
      @Html.RadioButtonFor(m => m.Contact, null)
</div>

And this is my Model (simplified) 这是我的模型(简体)

Public class Customer{
    //public string FirstName { get; set; }
    //public string LastName { get; set; }
    public string PrimaryNumber { get; set; }
    public string Mobile { get; set; }
    public string Contact { get; set; }
}

My problem is like this, how to I set the string value of the radio button to the input of the customer in the appropriate text box (in order for me to set it to the "Contact" property in my model) 我的问题是这样的,如何在适当的文本框中将单选按钮的字符串值设置为客户的输入(以便我将其设置为模型中的“ Contact”属性)

You could change the type from string to int and provide a value for each RadioButton: 您可以将类型从字符串更改为int,并为每个RadioButton提供一个值:

 @Html.RadioButtonFor(m => m.Contact, 1)
 @Html.RadioButtonFor(m => m.Contact, 2)

Or if you want to use a string: 或者,如果您想使用字符串:

 @Html.RadioButtonFor(m => m.Contact, "L")
 @Html.RadioButtonFor(m => m.Contact, "M")

By the way, this question has been already answered a google times, here are a couple of them: 顺便说一句,这个问题已经被谷歌时代回答过了,这里有几个:

RadiobuttonFor in Mvc Razor syntax RadiobuttonFor Mvc Razor语法

When using .net MVC RadioButtonFor(), how do you group so only one selection can be made? 使用.net MVC RadioButtonFor()时,如何分组以便只能进行一项选择?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM