Click here to Skip to main content
15,886,593 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My web page have a button and a form,I use @Html.BeginForm to produce the form and when I click the button,I will jump to the login page.To my surprise,when I click the button,the form post to my controller automatically.But when I use @using (Html.BeginForm,it seems OK.

My web page

HTML
 <button  >logout</button>
  @Html.BeginForm("ExportFile", "Student",  new { id = "exportForm" }))
{
@Html.Hidden("year")
@Html.Hidden("fileName")
}

My new web page will work well
HTML
 <button  >logout</button>
   @using (Html.BeginForm("ExportFile", "Student",  new { id = "exportForm" })))
{
@Html.Hidden("year")
@Html.Hidden("fileName")
}

I find the different HTML is System.Web.Mvc.Html.MvcForm):

HTML
<form id="exportForm" method="post" action="/Student/ExportFile">
System.Web.Mvc.Html.MvcForm) {
<input id="year" type="hidden" value="" name="year">
<input id="fileName" type="hidden" value="" name="fileName">
}

What is the difference between then?Someone teach me to add @{Html.EndForm();} when I using @Html.BeginForm,but it seems not work.May be it works in MVC3.
Posted
Updated 23-Dec-14 3:25am
v2
Comments
Philippe Mori 23-Dec-14 22:53pm    
I would think that using keyword would cause the object to be disposed and automatically call EndForm. You can easily try variation and look generated code or read the documentation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900