I am trying to create a form for creating a new entity. For that I have my viewmodel and that viewmodel contains property that is an ICollection:
public class AdminViewModel
{
public Article Article { get; set; }
public List<Article> Articles { get; set; }
}
public class Article
{
public Article()
{
Prices = new List<Price>();
}
[Column("Id")]
public int ArticleId { get; set; }
public string ArticleNumber { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Category> Category { get; set; }
public virtual ICollection<Price> Prices { get; set; }
}
And here is the controller that I am going to post to:
public ActionResult CreateProduct(Article article)
{
return View();
}
The problem:
I generate inputs in my form by using html helpers with razor, like so:
@Html.TextBoxFor(t => t.Article.Category, new { @class = "form-control" }))
All propertys in the model gets the correct values from the form, but not the one above that is ICollection.
I have read many blogs and articles about this topic but most of them only discuss how to go about it when the model is populated with data, not when it is empty, or examples of when you want to post a full collection of items and not ONE item contains a collection.