MVC 5 BeginCollectionItem with Partial CRUD

后端 未结 4 1366
夕颜
夕颜 2020-11-30 04:10

I have made changes below to the question, which is still the same but hopefully a lot clearer through the models and in regards to what I want to achieve and where I\'ve co

4条回答
  •  孤街浪徒
    2020-11-30 04:51

    Treat the views as independent. If your partial view was a full view, you would pass it a IEnumerable

    so render with:

     @Html.Partial("ClassBView", Model.ClassB)
    

    Also, you can't use foreach with EditorFor as there is insufficient information to create a name based on the index. Use a normal for loop instead. The expression parser can then convert it to name="name[1]" etc as the index is available in the expression:

    @model IEnumerable
    
    @if(Model != null)
    {
        for (int i = 0; i < Model.Count; i++)
        {
        
            
                @Html.EditorFor(modelItem => Model[i].Name)
                
                
            
        
        }
    }
    

    There is more missing from your example (what Clear and Create connect to for instance), so if you can provide the rest of your controller code I will expand this.

提交回复
热议问题