0

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.

  • Search for Html.EditorFor and EditTemplates. There are lots of examples even here on SO – Sir Rufo Jul 01 '17 at 17:29
  • Trying to bind a Collection to TextBox (used for scalar value handling)? Ideally you should be using a List supported control to display Category collection. Either you are going wrong on your requirement or you need to try [`DropDownList`](https://stackoverflow.com/questions/25036799/mvc5-view-drop-down-list) or for editable collection refer: [`EditorFor`](https://stackoverflow.com/questions/25333332/correct-idiomatic-way-to-use-custom-editor-templates-with-ienumerable-models-in) as suggested by @SirRufo in comment above. – Siva Gopal Jul 01 '17 at 17:44
  • Does this answer help you? https://stackoverflow.com/questions/20278362/editorfor-for-a-list-of-complex-type-mvc – Hopeless Jul 01 '17 at 23:17

0 Answers0