我有一个MVC3 Internet应用程序。如何为每个单选按钮保留不同的名称
基本上它有一个注册表单,其上有3个单选按钮。每当我检查一个单选按钮并按下按钮时,它应该到控制器并将数据插入到我的数据库中。
我还对我的Registration
表单中的字段进行了验证。
某处在stackoverflow
,我读过Property names in the Model and the Names of the fields in the view have to be matched
以便即使在验证错误后仍保留表格中的值。
此声明对于TextBoxes和Dropdownlists来说是完美的。
我的问题是:如何有不同的名称foreach radiobutton
?
正如我所说我的表单有3个单选按钮。
说,这些都是文本框,然后单选按钮
Phone 1 // here is the radio button1
Phone 2 // here is the radio button2
Phone 3 // here is the radio button3
在我的模型我已经
public string Phone1 { get; set; }
public string Phone2 { get; set; }
public string Phone3 { get; set; }
//also i have a preferred phone--which sets if any of the phone is selected
public bool PreferredPhone { get; set; }
我已经试过这样的事情
Phone1:
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone1, new { id = "Phone1", @onclick = "PrefferedPhone(this)" })
Phone2:
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone2, new { id = "Phone2", @onclick = "PrefferedPhone(this)" })
Phone3:
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone3, new { id = "Phone3", @onclick = "PrefferedPhone(this)" })
但这声明将所有单选按钮的名称设置为PreferredPhone
如何为每个单选按钮设置不同的名称?
即使我试图通过保持单选按钮的名称作为model=>model.Phone1
,model=>model.Phone2
,model=>model.Phone3
,但如果我这样设置,我可以能够选择所有的三个单选按钮。
基本上我需要的是,保持检查的单选按钮的值。我试过使用JavaScript,我已经在javascript中创建了一个函数来将选定的值存储在隐藏字段中,但这不起作用。所以,如果我具有不同的电台BTT名称,我可以将值从'view'传递到'Controller'。如果您有任何建议,请告诉我 –
Hi @Karthik我编辑了我的答案,也许这有助于吗? :) – gardarvalur
谢谢,但这给了我的首选电话价值作为'querystring'权利?为什么所有其他表单域都被捕获,为什么我的单选按钮被捕获? –