get value/SelectedValue from @Html.Editorfor on for loop

66 Views Asked by At

In the below code snippet, I want to get the value for "@Html.EditorFor" using Jquery. I have tried many ways to get. but I couldn't. Thank you for any inputs.

<table class="table table-striped" id="tblEvents">
             <thead>
                 <tr>
                     <th class="col-md-6 Subheading-2">Event Name</th>
                     <th class="col-md-6 Subheading-2">Event Date</th>
                 </tr>
             </thead>
             <tbody>
                 @for(int i=0;i<Model.ModelEvents.Count;i++)
                    {
                        <tr>
                            <td class="col-md-6">
                                @Html.HiddenFor(model=>model.ModelEvents[i].NAME)
                                @Html.HiddenFor(model=>model.ModelEvents[i].ADDRESS)
                                @Html.HiddenFor(model=>model.ModelEvents[i].PINCODE)
                                @Html.DropDownListFor(model=>model.ModelEvents[i].EVENTNAME,new SelectList(Model.Events,"Value","Text",Model.ModelEvents[i].EVENTNAME),new { @class = "form-control eventname", style = "max-width:300px" })
                                
                               
                             </td>
                            <td class="col-md-6">
                                @Html.EditorFor(model => model.ModelEvents[i].EVENTDATE, new { @class = "text  form-control eventdate" })
                            </td>

                        </tr>
                    }
                </tbody>
         </table>
1

There are 1 best solutions below

3
Patrick Hume On

EDIT


give the control an id and as it's in a loop include "i" to ensure a unique ID

new { @class = "text form-control eventdate", id = "foo_" + i }

you can then use this id as a reference for jquery to find

This will match any input whose ID starts with "foo_" so will match "foo_0", "foo_1", "foo_2" ect

$('input[id^="foo_"]').).each(function() {
  console.log($(this).val())
});

I hope this helps