I have a model that contains a list property List<EducationalBackground> EducationalBackground
and in the razor form I want the user to enter multiple institutions.
Model
public class Application
{
...
[Required(ErrorMessage = "Required Field")]
public List<EducationalBackground> EducationalBackground { get; set; }
...
public Application()
{
...
EducationalBackground = new List<Library.EducationalBackground>();
...
}
}
Here is the class:
public class EducationalBackground
{
public string InstituteName { get; set; }
...
}
In the razor view I am trying like this
@Html.TextBoxFor(m => m.EducationalBackground[0].InstituteName, new { @class = "form-control" })
but obviously it doesn't work because the list (EducationalBackground
) is empty.
The user will have a "Add New Institution" button, so the initial list size is unknown
How do I properly accomplish this?
UPDATE Found my solution. Look for my post in the answer section