ASP.NET MVC3 RenderPage & Html.BeginForm

I have a problem with RenderPage along with Html.BeginForm (I don't know what I'm doing wrong).

Suppose you have a simple _Test.cshtml, for example:

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

Then suppose you have a simple page like this one (which 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 call to the RenderPage helper, you will correctly receive a series of forms with the corresponding submit button. If you uncomment the RenderPage helper, no tag will be created. Not sure what's going on, can someone help me?

+3
source share
3 answers

Why are you using RenderPage? Html.Partialseems 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>   

( @{}, HTML):

<span>Test Text</span>
+2

Try changing your _Test.cshtml to

<span>Test Text</span>

And this is probably a matter of taste, but I prefer Html.Partialto RenderPage.

0
source

Source: https://habr.com/ru/post/1795254/


All Articles