I have a BookCreateModel which consists of book's plane info such as Title, PublishYear & etc plus a collection of book Authors (complex type) :
public class BookCreateModel
{
public string Title { get; set; }
public int Year { get; set; }
public IList<AuthorEntryModel> Authors { get; set; }
}
public class AuthorEntryModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
in CreateBook view I have used EditorFor
helper :
@Html.EditorFor(m => m.Authors, "AuthorSelector")
Edit1:
and AuthorSelector template is as below:
<div class="ptr_authors_wrapper">
@for (int i = 0; i < Model.Count; i++)
{
<div class="ptr_author_line" data-line-index="@i">
@Html.TextBoxFor(o => o[i].FirstName)
@Html.TextBoxFor(o => o[i].LastName)
</div>
}
</div>
<script>
...
</script>
the AuthorSelector
template contains some wrapper markups which need to be aware of each rendered item's index plus some javascript which handle the child input's interactions and need to be rendered once (inside the AuthorSelector
template), thus getting rid of the for loop/or the AuthorSelector template is not possible.
now the problem is EditorFor act a little strange and generate input names like this :
<input id="Authors__0__FirstName" name="Authors.[0].FirstName" type="text" value="" />
<input id="Authors__0__LastName" name="Authors.[0].LastName" type="text" value="" />
as you can see instead of generating names like Authors[0].FirstName
it adds an extra dot which makes the default model binder unable to parse posted data.
any idea ?
Thanks !