ASP.NET MVC如何将视频中的radiobutton选定值传递给控制器​​

时间:2015-11-30 00:35:17

标签: asp.net-mvc

我有一组动态生成的单选按钮。我只想将所选值传递给我的控制器。我尝试了一些东西,但它不起作用。我只能获取表单设置的隐藏字段值以用于不同的目的。 这些不起作用 - 它们什么都不返回:

var YourRadioButton2 = Request.Form["wc"];
string selectedTechnology = form["wc"];

有没有办法让选定的单选按钮到达控制器。这些radiobutton值是动态生成的。

视图

<%--wired choices--%>
         <tr > Wired Choices </tr>
        <% for (var i = 0; i < subform.wiredchoices.Count; i++) %>
        <%{ %> 
            <tr>  

                <td> <input type="radio" name="wc" value="<%:    subform.wiredchoices.ElementAt(i).WiredChoiceItem %>" > </td>
                <td><%:    subform.wiredchoices.ElementAt(i).WiredChoiceItem %> </td>

控制器

if (rcpt.Form.Is("VoIP"))
            {
                 Response.Write("<br />" + "wc " + wc);

                 List<String> listValues = new List<String>();
                 foreach (string key in Request.Form.AllKeys)
                 {                    
                         listValues.Add(Request.Form[key]);
                         Response.Write("<br />" + "first values  " + Request.Form[key]);
                 }

                 // Loop through every checkbox
                 foreach (var key2 in form.AllKeys)
                 {
                             Response.Write("<br />" + "second values  " + key2);
                 }


                //See Radiobutton selections
                var YourRadioButton = Request.Form["wc"];
                Response.Write("<br />" + "YourRadioButton " + YourRadioButton);
                string selectedTechnology = form["wc"];
                Response.Write("<br />" + "YourRadioButton " + selectedTechnology);

                var YourRadioButton2 = Request.Form["wlc"];
                Response.Write("<br />" + "YourRadioButton " + YourRadioButton2);
                string selectedTechnology2 = form["wlc"];
                Response.Write("<br />" + "YourRadioButton " + selectedTechnology2);

                Response.Write("<br />" + "VoIP Hire ");
                String wiredChoice = "test"; //assign value from radio button here
                String wiredLessChoice = "test"; //assign value from radio button here

                // Stor e submitting page to return to
                ViewData["ReturnUrl"] = fields.formType;



                </tr>
            <%} %>

1 个答案:

答案 0 :(得分:10)

您的代码让我想起过去使用网络表单的那些日子。啊!感谢上帝!。我们现在有ASP.NET MVC,我们可以在MVC中以干净的方式完成这项工作。

让我们开始为我们的视图创建一个视图模型。

public class Technology
{
    public int Id { set; get; }
    public string Name { set; get; }
}

public class CreateUserViewModel
{   
    [Required]
    public int? SelectedTechnology { set; get; }   

    public List<Technology> Technologies { set; get; }

    // Add other properties as needed by your view
    public string UserName { set; get; }
}

现在在您的GET操作方法中,创建此视图模型的对象,加载Technologies集合属性并将其发送到视图。

public ActionResult Create()
{
  var vm = new CreateUserViewModel();
  vm.Technologies = GetTechnologyList();
  return View(vm);
}
private List<Technology> GetTechnologyList()
{
    //Hard coded a list of items here, You may replace it with 
    // Items from your db table
    return new List<Technology>
    {
        new Technology {Id = 1, Name = "DSL"},
        new Technology {Id = 2, Name = "VOIP"},
        new Technology {Id = 3, Name = "Dialup"},
        new Technology {Id = 4, Name = "Magic"}
    };
}

现在在你的剃刀视图中,它是我们的CreateUserViewModel类的强类型

@model ReplaceYourNamespaceHere.CreateUserViewModel
@using (Html.BeginForm())
{
    @Html.ValidationSummary()

   <h4>Select a technology</h4>
    foreach (var item in Model.Technologies)
    {
       <div> @Html.RadioButtonFor(s => s.SelectedTechnology, item.Id) @item.Name</div>
    }
    <input type="submit"/>
}

这将使用Technologies集合中的项目的单选按钮呈现表单。提交表单时,所选技术的ID将设置为已发布表单/模型的SelectedTechnology属性。

[HttpPost]
public ActionResult Create(CreateUserViewModel model)
{
    if (ModelState.IsValid)
    {
        // Selected Technology Id is in model.SelectedTechnology
        // to do :Save and Redirect
    }
    //Reload the radio button list data
    model.Technologies = GetTechnologyList();
    return View(model);
}

相信我,这是:)

enter image description here

如果要在创建(或编辑)视图中预选一个radion按钮,可以将SelectedTechnology属性值设置为要选择的技术的ID。

 var vm = new CreateUserViewModel();
 vm.Technologies = GetTechnologyList();

 vm.SelectedTechnology = 3 ; //replace with a non-hard coded value 

 return View(vm);