开发者

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>
                        <inpu开发者_Python百科t 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?


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>


It's a bug I think:

https://connect.microsoft.com/VisualStudio/feedback/details/652944/html-beginform-output-not-rendered-if-preceded-by-renderpage


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜