Mario Segalla
Mario Segalla

Reputation: 107

get value from editorfor inside for loop

I'm kinda new to ASP.NET so i'm having some trouble to find out how I can do this. I have this problem where the user will give an amount(let's say 5) and then I have to create 5 textbox for the user to type some number in. My logic is, the user type the number amount and I create a html.editorfor foreach number.the problem is that I need the value from each editor.

like this.

 <div>
                  @for (int i = 0; i < Model.qtde; i++)
                  {
                        @Html.EditorFor(c => c.numeros, new
                           {
                               htmlAttributes =
                                new
                                {
                                    @class = "form-control input-lg",
                                    autofocus = true,
                                    @type = "number",
                                    min = 0,
                                    max = 1000
                                   
                                }
                           })
                @Html.ValidationMessageFor(model => model.numeros, "",
                new { @class = "text-danger" })
                  }
                </div>

qtde: number amount that the user typed.

numeros: the number that the user typed.

If more information is need I'll update accordingly.

Upvotes: 0

Views: 158

Answers (1)

Rutuja
Rutuja

Reputation: 460

Use following code which will give you correct values (numeros[i] will give you value of each editor) and declare numeros as List or array:

  @for (int i = 0; i < Model.qtde; i++)
      {
            @Html.EditorFor(c => c.numeros[i], new
               {
                   htmlAttributes =
                    new
                    {
                        @class = "form-control input-lg",
                        autofocus = true,
                        @type = "number",
                        min = 0,
                        max = 1000
                       
                    }
               })
    @Html.ValidationMessageFor(model => model.numeros[i], "",
    new { @class = "text-danger" })
      }
            

Upvotes: 1

Related Questions