MVC HtmlHelper Usage (i) @Html. Summary of the use of BeginForm

Source: Internet
Author: User

[Email protected] (Html.BeginForm ()) {}//submit to current page

[Email protected] (Html.BeginForm (New {})) {}//submit to current page, new can take parameters

[Email protected] (Html.BeginForm ("Action", "Controller")) {}//submitted to the specified controller

[Email protected] (Html.BeginForm ("Action", "Controller", FormMethod.Post))//Specify the method of submission

Note: All content to be submitted including the button must be within {}

Example

1. Specify the form submission path and method:

@using (Html.BeginForm ("Index", "Home", formmethod.get,new{name = "Nbform", id= "Idform"})) {}

HTML format:

<form name= "Nbform" id= "Idform" action= "/home/index" method= "get" > </form>

2. Specify how the form is submitted as a file:

@using (Html.BeginForm ("Importexcel", "Home", formmehod.post,new {enctype= "Multipart/form-data"})) {}

HTML format: slightly

Note that it is sometimes necessary to add {id=1} or the index of the current page is automatically appended to the form when you click on the index above the first page, and if you search at this point, you may get an "out of index value" error message
@using (Html.BeginForm ("Index", NULL, new {id = 1}, Formmethod.get))

3. Add new {area = string. Empty} is to prevent the commit link from being followed by a parameter, in a custom way, as can be written as new {id = ""}

@using (Html.BeginForm ("Shipping", "Order", new {area = string. Empty}, FormMethod.Post, new {id = "Shippingform"})

[Email protected] (Html.BeginForm ("Index", "Home", Formmehod.method)) {} can also write

<% using (Html.BeginForm ("Index", "Home", Formmethod.method)) {%>

<%}%>

FormMethod for enumeration mode

public enum FormMethod

{

Get=0,

Post=1,

}

5. How the controller accepts parameters

Ex: in aspx

@using (Html.BeginForm ("Index", "Home", Formmehod.post))

{

@Html. TextBox ("username")

@Html. TextBox ("password")

<input type= "Submit" value= "Login"/>

}

In the controller

Public ActionResult Index ()

{

String struser=request.form["username"];

String strpass=request.from["password"];

viewdata["V"]= "your account is" +struser+ "password is" +strpass;

return View ();

}

Accept the value of a pass in index.aspx

<%=viewdata["V"]%>

Ex: in MVC

@using (Html.BeginForm ("Shipping", "Order", new {area = string. Empty}, FormMethod.Post, new {id = "Shippingform"})

{

@Html. Validationmessage ("Shipinfo.delivertype")
@Html. hiddenfor (M = m.shipinfo.delivercountry)
@Html. hiddenfor (M = m.shipinfo.deliverprovince)
@Html. hiddenfor (M = m.shipinfo.delivercity)
@Html. hiddenfor (M = m.shipinfo.delivercityarea)

}

1. The HTML tag name is the same as the parameter name

Public ActionResult Shipping (string shipinfo.delivertype,string shipinfo.delivercountry,string Shipinfo.deliverprovince,string shipinfo.delivercity,string shipinfo.delivercityarea) {}

2.HTML Label the Name property is consistent with the model property

Public ActionResult Shipping (Models.ViewModels.Order.OrderViewModel model) {}

3. Form collection Parameters

Public ActionResult Shipping (FormCollection FC)

{

String strresult=fc["Selectoption"]; Collection parameters to obtain data in the form of key-value pairs

}

MVC HtmlHelper Usage (i) @Html. Summary of the use of BeginForm

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.