【问题标题】:ASP.NET MVC How to Pass radiobutton selected value from view to controllerASP.NET MVC 如何将单选按钮选定的值从视图传递到控制器
【发布时间】:2015-11-30 09:35:00
【问题描述】:

我有一组动态生成的单选按钮。我只想将选定的值传递给我的控制器。我尝试了几件事,但它不起作用。我只能获取表单为不同目的设置的隐藏字段值。 这些不起作用——它们什么也不返回:

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

有没有办法可以将选定的单选按钮传递给控制器​​。这些单选按钮值是动态生成的。

视图

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

【问题讨论】:

  • U 需要在带有 Button 的表单内有单选按钮,当 Button 被触发时,单选按钮的值应该被发送到控制器,否则我认为 Ajax 也是一个很好的使用习惯。但是然后你将值发布到 Ajax 并从 Ajax 发布到控制器。

标签: asp.net-mvc


【解决方案1】:

您的代码让我想起了过去使用 Web 表单的日子。啊!感谢上帝!。我们现在有 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"}
    };
}

现在在您的 Razor 视图中,它是我们的 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);
}

相信我,这里是:)

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

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

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

 return View(vm);

【讨论】:

  • 您好,感谢您的指导。这看起来很有希望。在我开始实施之前,我必须问一下您在 razor 中编写的视图是否仍然可以在 aspx 中工作?因为我的项目很大,并且是用 aspx 而不是 razor 完成的。你能提供同样的 aspx 语法吗?
  • 很棒的解决方案,为我完美修复!我总是告诉自己,如果我的模型设置正确,一切都会像融化的巧克力河一样流动!谢谢Shyju!
猜你喜欢
  • 1970-01-01
  • 2019-03-24
  • 2017-11-30
  • 2020-09-02
  • 2018-07-12
  • 2020-03-12
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
相关资源
最近更新 更多