Create a form from a recursive model

后端 未结 1 1351

I have a recursive model like this:

public class Node
{
    public int Id { get; set; }
    public string Text { get; set; }
    public IList Chi         


        
相关标签:
1条回答
  • 2020-12-17 01:33

    You could use editor templates which respect the current navigational context instead of such @helper.

    So define a custom editor template for the Node type ( ~/Views/Shared/EditorTemplates/Node.cshtml):

    @model Node
    <li>
        @Html.LabelFor(x => x.Text)
        @Html.EditorFor(x => x.Text)
        @if (Model.Childs.Any())
        {
            <ul>
                @Html.EditorFor(x => x.Childs)
            </ul>
        }
    </li>
    

    and then inside some main view:

    @model MyViewModel
    <ul>
        @Html.EditorFor(x => x.Menu)
    </ul>
    

    where the Menu property is obviously of type Node.

    0 讨论(0)
提交回复
热议问题