2012-11-28 36 views
0

我有一个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.Phone1model=>model.Phone2model=>model.Phone3,但如果我这样设置,我可以能够选择所有的三个单选按钮。

回答

0

当你谈论选择所有三个单选按钮时,它听起来像你正在谈论他们,就好像他们是复选框。在同一组中考虑的单选按钮不能同时全部选中,每组只选一个(但也许你已经知道这一点,但我误解了)。

从您在Stackoverflow中读取的某处正确显示模型中的属性名称和视图中字段的名称必须匹配。富勒例如,如果你有这样的观点单选按钮:

@Html.RadioButton("stuffRadio", 1) 
@Html.RadioButton("stuffRadio", 2) 
@Html.RadioButton("stuffRadio", 3) 
@Html.RadioButton("stuffRadio", 4) 

然后在当窗体已经公布的控制器:

[HttpPost] 
public ActionResult TheAction(Model model, string stuffRadio) 
{ 
    //dostuff to model and other things 
    string value = stuffRadio; 
    //dostuff to the selected radioStuff :) 
} 

无论如何,如果I'm然后misunderstang我要问你作为回报。你说你想要区分每个单选按钮,我不太清楚为什么。是这样你可以得到所有的后置动作电话号码和控制器?

用我自己的例子,我只想做这样的事情:

@Html.RadioButton("stuffRadioGroup1", 1) 
@Html.RadioButton("stuffRadioGroup2", 2) 
@Html.RadioButton("stuffRadioGroup3", 3) 
@Html.RadioButton("stuffRadioGroup4", 4) 

但话又说回来,如果我要读所有的公益组选择我当然会已经做的参数同样的事情到控制器是这样的:

[HttpPost] 
public ActionResult TheAction(Model model, 
    string stuffRadioGroup1, string stuffRadioGroup2, 
    string stuffRadioGroup3, string stuffRadioGroup4) 
{ 
    //dostuff to model and other things 
    string value1 = stuffRadioGroup1; 
    string value2 = stuffRadioGroup2; 
    string value3 = stuffRadioGroup3; 
    string value4 = stuffRadioGroup4; 
    //dostuff to the selected radioStuff :) 
} 

再次,可以I'm misunderstang,然后你当然指正;) 后来!

########## EDITED ###################

首先,如果你想隐藏字段被提交,他们必须在表格内,并且该特定隐藏字段的名称必须作为参数传入具有相同名称的控制器中。例如:

@(Html.BeginForm("SomeAction", "Controller") 
{ 
    <input type="hidden" name="derp" value="1234" /> 
    @Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone3, new { id = "Phone3", @onclick = "PrefferedPhone(this)" }) 
    <button type="submit">submit</button> 
} 

如果隐藏字段应该是通过为好,控制器应该是这样的:

[HttpPost] 
public ActionResult SomeAction(Model mymodel, string derp, FormCollection collection) 
{ 
    int number = Convert.toInt32(derp); 
    //now number should be '1234' 

    string phone3 = collection.Get("Phone3"); 
    //now you have the phone number and the hidden field telling you if that number is preffered 

    TryUpdateModel(mymodel); //populate the model if it hasn´t already been populated 

    if(ModelState.IsValid) 
    { 
     //do stuff 
    else 
    { 
     //TODO: here you should set the model with the stuff from derp and collection before returning 
     return View("SameViewAsBefore", mymodel); 
    } 
} 

正如你可以看到我放的FormCollection在那里,只是有一种不同的方法。希望这可以帮助,如果没有,你可以随时要求再次:) (I'm知道有时候误解的东西)

+1

基本上我需要的是,保持检查的单选按钮的值。我试过使用JavaScript,我已经在javascript中创建了一个函数来将选定的值存储在隐藏字段中,但这不起作用。所以,如果我具有不同的电台BTT名称,我可以将值从'view'传递到'Controller'。如果您有任何建议,请告诉我 –

+0

Hi @Karthik我编辑了我的答案,也许这有助于吗? :) – gardarvalur

+0

谢谢,但这给了我的首选电话价值作为'querystring'权利?为什么所有其他表单域都被捕获,为什么我的单选按钮被捕获? –

相关问题