接上篇,我們下面通過HtmlFiledSet helper來展示何時使用EndView():
如果你用Asp.net已經有一段時間了,那使用Html.BeginForm helper來創建HTML form標簽的方式會讓 你覺得有點怪.當你創建一個新的Asp.net mvc項目後,在View裡的ChangePassword.aspx會默認被創建, 這個頁面使用了Html.BeginForm helper,下面是使用這個helper的代碼段:
<% using (Html.BeginForm()) { %>
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="currentPassword">Current password:</label>
<%= Html.Password("currentPassword") %>
<%= Html.ValidationMessage("currentPassword") %>
</p>
<p>
<label for="newPassword">New password:</label>
<%= Html.Password("newPassword") %>
<%= Html.ValidationMessage("newPassword") %>
</p>
<p> <label for="confirmPassword">Confirm new password:</label>
<%= Html.Password("confirmPassword") %>
<%= Html.ValidationMessage("confirmPassword") %>
</p>
<p>
<input type="submit" value="Change Password" />
</p>
</fieldset>
</div>
<% } %>
上面代碼中你會發現Html.BeginForm的使用和<%=Html.Password(“currentPassword”) %>的 使用不盡相同,也就是Html.BeginForm在using語句中被調用,這點很有意思,讓我先來看看上面代碼段生 成後的Html,如下:
<form
action="/Account/LogOn?ReturnUrl=%2fAccount%2fChangePassword"
method="post">
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<input id="username" name="username" type="text" value="" />
</p>
<p>
<label for="password">Password:</label>
<input id="password" name="password" type="password" />
</p>
<p>
<input id="rememberMe" name="rememberMe" type="checkbox" value="true" />
<input name="rememberMe" type="hidden" value="false" />
<label class="inline" for="rememberMe">Remember me?</label>
</p>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
</form>