Here are my models
public class AddressBook
{
public string UserId { get; set; }
public IList<Address> Addresses { get; set; }
public AddressBook()
{
UserId = "";
Addresses = new List<Address>();
}
}
public class Address
{
public string Company { get; set; }
public string Title { get; set; }
public string FName { get; set; }
...
}
The controller builds the AddressBook with a list of addresses. The main page uses the AddressBook model (@model mymodel.AddressBook) and I can access the different addresses using Model.Addresses[index].
On the page I display the list of addresses each with an Edit button (I stripped the html code off for clarity):
@model mymodel.AddressBook
...
@for (int i = 0; i < Model.Addresses.Count; i++)
{
@Model.Addresses[i].Company
@Model.Addresses[i].FName
...
@:<input type="image" src="/images/edit.gif" onclick="addressEdit('@i'); return false;" title="Edit" />
}
When the user clicks on the edit button I call javascript addressEdit and pass it the index of the selected address.
<script type="text/javascript">
function addressEdit(index) {
$('#FName').val('@Model.Addresses[index].FName');
$('#Addr1').val('@Model.Addresses[index].Company');
...
}
</script>
The problem is on the jQuery lines $('#FName').val('@Model.Addresses[index].FName'); Variable index is underlined in red in VS2012 with message "the name 'index' does not exist in the current context".
How do you pass the value on 'index' to extract the data I need?