【问题标题】:Passing multi parameters (radio) from view to controller using array使用数组将多个参数(无线电)从视图传递到控制器
【发布时间】:2023-04-11 04:24:01
【问题描述】:

我想在不使用模型的情况下将 55 到 100 个从视图中可能是假或真的项目发布到我的控制器。这是我在 Razor View 中的代码:

@using(Html.BeginForm("User","User",FormMethod.Post,new{enctype="multipart/form-data"}))
    {

        <input type="radio" name="answer1">Choice one</input>
        <input type="radio" name="answer1">Choice two</input>
        <input type="radio" name="answer2">Choice three</input>
        <input type="radio" name="answer2">Choice four</input>

    ....

    <input type="radio" name="answer55">Choice fifty five</input>
    <input type="radio" name="answer55">Choice fifty six</input>

    <button type="submit">Send</button>
                                   }

控制器:

[HttpPost]
public async Task<ActionResult> User(Array....)
{
return view();}

如何使用数组将所有参数(单选按钮)发送到我的控制器。感谢您提供的所有解决方案。

【问题讨论】:

  • 请参阅您上一个问题中的 cmets - 您没有为单选按钮提供 value

标签: c# asp.net-mvc razor asp.net-mvc-5


【解决方案1】:

也许创建一个包含size bool 元素数组的视图模型类,然后将每个元素绑定到 Razor 页面中的单选按钮。

public class BooleanViewModel
{
   public BooleanViewModel(int size)
   {
      BoolArray = new bool [size];
   }

   public bool [] BoolArray {get; set;} 
}

设置你的 Razor 页面模型:@model BooleanViewModel

然后,在 Razor 中,您可以使用 @Html.RadioButtonFor(model =&gt; model.BoolArray[0], "textForButton") 等。 您还可以创建一些 foreach 循环并遍历 model.BoolArray 中的所有项目。

【讨论】:

  • 布尔数组的大小,55、100 或任何你想要的大小。
  • 根据我的规范,我坚持在这种特殊情况下不使用 Viewmodel。我知道 ViewModel 是正确的方式,但在这种情况下不是。
  • 你现在的型号是什么?
【解决方案2】:

试试它: 在您的控制器中:

 // GET: Home
        public ActionResult Index()
        {
            List<MyItem> radioItems = new List<MyItem>();
            radioItems.Add(new MyItem() { Caption="radio1", Value="radio1Value"});
            radioItems.Add(new MyItem() { Caption="radio2", Value="radio2Value"});
            radioItems.Add(new MyItem() { Caption="radio3", Value="radio3Value"});
            radioItems.Add(new MyItem() { Caption="radioN", Value="radioNValue"});
            return View(radioItems);
        }
        [HttpPost]
        public void IndexPost(List<string> items)
        {
            // todointo item has your selected item
            var x = items;

        }

在你的cshtml中:

@using (Html.BeginForm("IndexPost", "Home", FormMethod.Post))
        {
            foreach (var item in Model)
            {

                <input type="checkbox" name="items" value="@item.Value">@item.Caption
            }

            <button Type="submit">Send</button>

        }

【讨论】:

    【解决方案3】:

    在视图中:要使数组起作用,请按以下格式命名单选按钮以使模型绑定起作用

    @using (Html.BeginForm("Index", "Home")) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary()
        <input type="radio" name="param[0]" value="one" >
        <input type="radio" name="param[0]" value="two" >
        <input type="radio" name="param[1]" value="three" >
        <input type="radio" name="param[1]" value="four" >
        <input type="radio" name="param[2]" value="five" >
        <input type="radio" name="param[2]" value="six" >
      <input type="submit" value="submit" />
    }
    

    在 Post ActionMethod 中:

     [HttpPost]       
     public ActionResult Index(string[] param)
                    { //
        }
    

    然后你会在action方法中得到param数组中的值。 您可以拥有任意数量的单选按钮,ModelBinder 将负责绑定。

    【讨论】:

      猜你喜欢
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多