Model Binding to a List using non-sequential indexes. Can I access the index later?
I'm following Phil's great tutorial on model binding to a list.
I use input names like this:
开发者_如何学运维book[5804].title
book[5804].author
book[1234].title
book[1234].author
This works well and the data gets back to the model just fine, populating a list of books.
What I'm looking for is a way to get access in the model to the index that was used to send the books. I'd like to get that number, "5804." This is because the index is of semantic importance. If I can access it, it saves me from setting another property on the object (book ID).
Is there a way to see, either on the FormCollection or on the model after UpdateModel is called, what the index was when it was sent up?
If the index has semantic importance put it into the model:
book[0].id = "5804"
book[0].title = "title 1"
book[0].author = "author 1"
book[1].id = "1234"
book[1].title = "title 2"
book[1].author = "author 2"
The answer to this question, in case anyone else is looking to do it, is "no, you can't access the index later."
精彩评论