0

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
  • Refer the comments in your previous question - your not giving your radio buttons a value Commented Jun 5, 2017 at 12:02

3 Answers 3

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Size of your bool arrays, 55, 100 or any size you want.
According to my specification I insist to not use Viewmodel in this particular case. I understand that ViewModel is the propper way but not in this case.
What is your model now?
0

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>

        }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.