Roberto
Roberto

Reputation: 143

ASP.NET MVC3 RenderPage & Html.BeginForm

I've a problem with RenderPage together with Html.BeginForm (don't know what i'm doin wrong).

Suppose you have a simple _Test.cshtml such as this:

@{
    <span>Test Text</span>
}

Then suppose you have a simple page like this (wich uses _Test.cshtml):

 @{
    Layout = null;
    var b = new int[] { 0, 1, 2, 3, 4 };
}

@{
    <html>
        <body>
            @* @RenderPage("~/Views/Shared/_Test.cshtml") *@
            <div>
                @foreach (int i in b)
                {
                    <div>
                    @using (Html.BeginForm("Action", "Controller", new { id = i }, FormMethod.Post, new { id = "frm_"+ i.ToString() })) 
                    {
                        <span>Label&nbsp;&nbsp;</span>
                        <input type="submit" id="@i.ToString()" value="@i.ToString()" />
                    }
                    </div>
                }
            </div>
        </body> 
    </html>   
}

If you comment out the RenderPage helper call you correctly get a series of form with the corresponding submit button. If you uncomment the RenderPage helper no tag is generated. Don't know what's going on, may someone help me?

Upvotes: 3

Views: 3609

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Why are you using RenderPage? Html.Partial seems more native:

@{
    Layout = null;
    var b = new int[] { 0, 1, 2, 3, 4 };
}
<html>
    <body>
        @Html.Partial("~/Views/Shared/_Test.cshtml")
        <div>
            @foreach (int i in b)
            {
                <div>
                @using (Html.BeginForm("Action", "Controller", new { id = i }, FormMethod.Post, new { id = "frm_"+ i.ToString() })) 
                {
                    <span>Label&nbsp;&nbsp;</span>
                    <input type="submit" id="@i.ToString()" value="@i.ToString()" />
                }
                </div>
            }
        </div>
    </body> 
</html>   

and also your partial (no need of those server side bocks @{} when you have static HTML):

<span>Test Text</span>

Upvotes: 2

Andy Gaskell
Andy Gaskell

Reputation: 31761

Try changing your _Test.cshtml to

<span>Test Text</span>

And this is probably a matter of taste but I prefer Html.Partial to RenderPage.

Upvotes: 0

Related Questions