1

I have the following:

_ImageGalleryPartial

@if (Model != null)
{
    foreach (var item in Model)
    {
        @Html.Partial("_ImageItem", item);
    }
}

_ImageItemPartial

@model WebsiteEngine.Models.PortfolioImage

@{
    string url = VirtualPathUtility.ToAbsolute(String.Format("~/{0}", Html.DisplayFor(m => m.FileName)));
}

@using (Html.BeginForm("DeleteImage", "Portfolio", FormMethod.Post, new { @class = "deleteImageForm" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true);

    @Html.HiddenFor(m => m.Name)
    @Html.HiddenFor(m => m.FileName)
    @Html.HiddenFor(m => m.Order)
    @Html.HiddenFor(m => m.PortfolioID)
    @Html.HiddenFor(m => m.PortfolioImageID)

    <div class="span3">
        <div class="item">
            <a class="fancybox-button" data-rel="fancybox-button" title="Photo" href="@url">
                <div class="zoom">
                    <img src="@url" alt="Photo" />                    
                    <div class="zoom-icon"></div>
                </div>
            </a>
            <div class="details">
                <a href="#" class="icon deleteImage" data-form=""><i class="icon-remove"></i></a>
            </div>
        </div>
    </div>
}

When I inspect the page elements using Chrome dev tools, the first element is missing the surrounding form. If I inspect the page source (using right) click then the form is there. This lead me to believe some JS was removing the form so I disabled JS but it was still missing.

If change _ImageGalleryPartial so it's like this (notice the addition of an empty model):

@if (Model != null)
{
    @Html.Partial("_ImageItem", new WebsiteEngine.Models.PortfolioImage());
    foreach (var item in Model)
    {
        @Html.Partial("_ImageItem", item);
    }
}

Then all the "correct" elements get a form but again, this first item doesn't.

I'm still inclined to think this is a JS issue but I'm a little stumped as disabling JS doesn't fix it. Is this some off behaviour with MVC?

Just to note, I have simplified my layout above, I do actually have one or 2 nested forms but assume that's Ok as everything else is Ok, it's just this first partial that's broken.

Any ideas?

tereško
  • 58,060
  • 25
  • 98
  • 150
webnoob
  • 15,747
  • 13
  • 83
  • 165

1 Answers1

1

Html forms can't be nested.

Chrome will ignore illegal tags, thus they are not showing.

You can read this post for further information.

Community
  • 1
  • 1
Yan Brunet
  • 4,727
  • 2
  • 25
  • 35
  • I was thinking this as well but seeing as the others were working OK I assumed all was fine. I will take a look into this when I get home. – webnoob Oct 03 '13 at 07:16
  • Thanks Yan, although I knew about multiple forms, your answer made me focus my efforts on re-writing how my forms worked which has resolved the issue. – webnoob Oct 04 '13 at 11:29