简体   繁体   中英

ASP.NET MVC 3 - can't properly add controls in a helper method

I feel like I'm missing something obvious here. I just wanted to try putting out some simple XML output onto a screen, so I have my model bringing in an xml file to the view as an XElement in a simple mvc3 app. The problem is that none of the html from the helper method is being generated. If I try to render the same elements in the regular foreach loop code it works fine, but it's almost as though it's completely skipping over the helper, or just not caring enough to render the element.

Is there something simple I'm missing?

@helper NavigateElement(XElement xElement)
{
    if (xElement.HasElements)
    {
        foreach (XElement xChildElement in xElement.Elements())
        {
            NavigateElement(xChildElement);
        }
    }
    else
    {
        @Html.Label(xElement.Name.LocalName.ToString())
        @Html.TextBox(xElement.Name.ToString(), xElement.Value)
    }
}


@foreach (XElement xElement in Model.exampleXML.Elements())
{
    if (xElement.HasElements)
    {
        foreach (XElement xChildElement in xElement.Elements())
        {
            NavigateElement(xChildElement);
        }
    }
    else
    {
        @Html.Label(xElement.Name.LocalName.ToString())
        @Html.TextBox(xElement.Name.ToString(), xElement.Value)
    }
}

shouldn't your else blocks look like this?

else
{
    Html.Label(xElement.Name.LocalName.ToString())
    Html.TextBox(xElement.Name.ToString(), xElement.Value)
}

您实际上在任何地方打电话给您的助手吗?

@NavigateElement(Model.exampleXML)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM