将RadioButton值传递给控制器

时间:2015-08-28 18:27:04

标签: asp.net-mvc asp.net-mvc-3 razor

我正在尝试将radiobutton值传递给控制器​​。但是,我似乎无法弄清楚如何获得我通过的价值。我不知道如何处理控制器。

我用来保存值的模型

public class PendingTasksModel
{
    public string environmentName { get; set; }
}

查看

<div class="col-md-3">
    <div class="btn-group-vertical pull-right" data-toggle="buttons">
         <label class="btn btn-primary" style="@displayOak">
             @Html.RadioButtonFor(model => model.environmentName, "OAK")
             OAK
         </label>
         <label class="btn btn-primary" style="@displayQa">
             @Html.RadioButtonFor(model => model.environmentName, "QA")
             QA
         </label>
         <label class="btn btn-primary" style="@displayProd">
             @Html.RadioButtonFor(model => model.environmentName, "PROD")
             PROD
         </label>
     </div>
 </div>

控制器

public ActionResult InitializeService(PendingTaskModel model)
    {
        _binding = new BasicHttpBinding();
        if (model.environmentName == "OAK")
        {
            _endpointAddress = new EndpointAddress(ConfigurationManager.AppSettings["OAK"]);
        }
        else if (model.environmentName == "PROD")
        {
            _binding.Security.Mode = BasicHttpSecurityMode.Transport;
            _endpointAddress = new EndpointAddress(ConfigurationManager.AppSettings["PROD"]);
        }
        else
        {
            _binding.Security.Mode = BasicHttpSecurityMode.Transport;
            _endpointAddress = new EndpointAddress(ConfigurationManager.AppSettings["QA"]);
        }
        _channel = new ChannelFactory<IEisIntegrationService>(_binding, _endpointAddress);

        try
        {
            _target = _channel.CreateChannel();


        }
        catch
        {
            if (_target != null)
            {
                ((ICommunicationObject)_target).Abort();
            }
        }
        return View();
    }

1 个答案:

答案 0 :(得分:3)

为了让MVC基础架构自动将表单字段连接到控制器,您需要确保参数名称和类型匹配。尝试将string option参数更改为PendingTasksModel model,然后在if语句中查看model.environmentName。或者,将string option更改为string environmentName