Html.BeginForm方法沒有產生Form標籤的問題

來源:互聯網
上載者:User

在做一個mvc3項目的時候,發現有的頁面的form標籤沒有產生(用@Html的方法),於是不得不手寫原生的form標籤,今天決定把問題找一找。

我的頁面是這樣的,一個layout頁,裡面有幾個RenderSection,一個RenderBody

<body>    @RenderPage("~/Views/Shared/_top.cshtml")    @if (IsSectionDefined("topnav"))    {         @RenderSection("topnav");    }    <!--main start-->    @RenderBody()    <!--main end-->    @if (IsSectionDefined("friendlink"))    {         @RenderSection("friendlink", false);    }    @RenderPage("~/Views/Shared/_Footer.cshtml")    @if (IsSectionDefined("footjs"))    {        @RenderSection("footjs", required: false);    }</body>

然後在頁面裡我開始寫@using(Html.BeginForm()){}

怎麼寫都不出來,於是開始最小化測試,把所有代碼都刪掉,寫了beginform,成功

然後開始一點點地加代碼,最終找到問題,我的內頁是這樣的:

@section topnav{    @RenderPage("~/Views/Shared/_headtab.cshtml", new { current = "guahao" })    @RenderPage("~/Views/Shared/_nav.cshtml", new { step = "測試人員中樞" })}<div class="u_leftbar">    @RenderPage("~/Views/Shared/Wedget/_Guide.cshtml")    @RenderPage("~/Views/Shared/Wedget/_ReserveType.cshtml")</div><div id="main">@using(Html.BeginForm()){    //....}<div>

可見,我render了四個page,問題就出在這上面,這四個partialview有什麼區別?

區別在,在section裡面的partialview是在RenderBody之外的由RenderSection方法渲染的,而寫在div標籤內的卻是由RenderBody渲染的,當我把這兩個RenderPage寫到section裡面去,Form標籤果然出現了。

這當然沒解決問題,section中是佔位,並不保證跟body裡面的內容排版關聯,當然不能簡單地把RenderPage都丟到section裡面去,而且這樣的話也讓這種模組化的布局推動了意義,本來就是用載入部分視圖的方式輕鬆載入各種掛件,還寫html代碼顯然不適合,於是我用了載入部分視圖的方法,解決了問題:

<div class="u_leftbar">    @Html.Partial("~/Views/Shared/Wedget/_Guide.cshtml")    @Html.Partial("~/Views/Shared/Wedget/_ReserveType.cshtml")</div>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.