Passing multi parameters (radio) from view to controller using array

痞子三分冷 提交于 2019-12-25 14:48:38

问题


I would like to post between 55 and 100 items which could be false or true from view to my controller without using Model. This is my code in 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>
                                   }

Controller:

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

How can I send all of parameters (radio buttons) using an array to my controller. I appreciate all your solutions.


回答1:


Maybe create a view model class that contains an array of size bool elements, and then bind each of element to the radio button in Razor page.

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

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

Set your Razor page model: @model BooleanViewModel

Then, in Razor you could use @Html.RadioButtonFor(model => model.BoolArray[0], "textForButton") etc. You could also make some foreach loop and go through all items within model.BoolArray.




回答2:


Try its: in your controller:

 // 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;

        }

in your 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:


In View :For Arrays to work, name the Radio buttons in format as below for model binding to work

@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" />
}

In Post ActionMethod :

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

then you will get the value in param array in the action method. You can have as many number of Radio buttons as you wish & ModelBinder will take care of the binding.



来源:https://stackoverflow.com/questions/44367863/passing-multi-parameters-radio-from-view-to-controller-using-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!