Using built-in HTML helper methods in MVC to generate form elements to submit forms and traps for button event

Source: Internet
Author: User

The Site template page has a login exit button that jumps to the landing page when clicked.

<button onclick="logout ()" > Exit </button> $ ("#logOut  "). Click (function () {  "@Url. Action ("Logout" "Account ")            ;  
View Code

Then a page landlord uses the HTML helper method to produce form elements, the code is as follows:

@Html. BeginForm ("Returnfile","File", FormMethod.Post,New{id ="Exportform" }))    {    <input type="Hidden"Id="ExportString3"Name="ExportString3"/> <input type="Hidden"Id="FileName3"Name="FileName3"/>    }
View Code

If the form is not present on the page, then the Exit button is used normally, but when both appear, the Click button event jumps to the controller that the form points to. Because on the other page exit does not appear this problem, so the landlord thought is a form problem ...

Start by querying the HTML helper method to produce the form element, the HTML code displayed on the interface is as follows (a bunch of System.Web.Mvc.Html.MvcForm)):

<form id="Exportform"Method="Post"action="/file/returnfile">System.Web.Mvc.Html.MvcForm) {<input id="exportstring"Type="Hidden"Name="exportstring"><input id="FileName"Type="Hidden"Name="FileName">}</form>
View Code

It is no problem if you use the form element directly.

Workaround 1 (use HTML elements directly):

<form method="POST"action="@Url. Action ("Returnfile", "File")"Id="Exportform"> <input type="Hidden"Id="exportstring"Name="exportstring"/> <input type="Hidden"Id="FileName"Name="FileName"/> </form>
View Code

After work to check the data, found that the button does not show the type of definition, in different browsers will have different properties, so be sure to add type= "button."

The Type property specifies the kind of button. Tip: Always specify the Type property for the button. The default type of Internet Explorer is "button", while the default value for other browsers, including the specifications, is "submit".

In addition the landlord's original form quoted method error, incredibly no error. The correct method of use is as follows:

A. Use@Html.BeginForm,@Html.EndForm(自定义id和name),网上说要用Html.EndForm()来关闭</form>表单,但是楼主测试的时候IE和火狐不加也会关闭

@{Html.BeginForm ("Returnfile","File",New{@id =string. Empty},formmethod.post,New{id ="Exportform", name ="Exportform" }); } {@Html. Hidden ("exportString1") @Html. Hidden ("fileName1")    }  //not to add or@{html.endform ();}
View Code

B. Common use of @using method

@using (Html.BeginForm ("Returnfile","File",New{id ="Exportform" }))    {        <input type="Hidden"Id="exportstring"Name="exportstring"/> <input type="Hidden"Id="FileName"Name="FileName"/>    }
View Code

Using built-in HTML helper methods in MVC to generate form elements to submit forms and traps for button event

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.