In the view page, we need to construct an HTML page to submit form data or display form data. For a page like HTML, we used to directly drag ASP: Server Control.
But now in MVC, how is it done?
1. We need to understand the HTML. beginform () and HTML. Textbox () HTML auxiliary methods.
The HTML. beginform () auxiliary method is used to output HTML <form> elements. The method is as follows:
<% Using (HTML. beginform () {%> HTML <%} %> left brackets indicate the start of from, right brackets indicate the end of from, and the middle is the content to be placed in from.
This method uses using before. If you do not want to use it, you can also use HTML. beginform () and HTML. endform () combinations.
Code example:
<% HTML. beginform (); %> HTML content <% HTML. endform (); %> the generated Page code: <form action = "/dinners/edit/1" method = "Post"> </form>
2. With <form>, we need to talk about the tag control and the HTML. Textbox () auxiliary method.
The Edit. aspx view outputs <input type = "text"/> elements using the HTML. Textbox () method:
<% = Html. Textbox ("title") %>
The preceding html. Textbox () method receives only one parameter-used to specify the ID/Name of the <input type = "text"/> element at the same time.
Attribute, and fill in the model attribute of the text box value. For example, the title attribute value of the input dinner object is ". Net futures", because
ThisHtml. Textbox ("title ")Method to output <input id = "title" name = "title" type = "text" value = ". Net ures"/>.
In addition, we can also use the first parameter of HTML. Textbox () to specify the element's ID/name attribute value, and explicitly give the second parameter
Number of passed values.
<% = Html. Textbox ("title", model. Title) %>
To format the output result, you can use the string. Format () Static Method built in. net. Edit. aspx View
The template uses this method to format the eventdate value (datetime type) without displaying the second information:
Formatting time: <% = html. Textbox ("eventdate", String. Format ("{0: g}", model. eventdate) %>
The third optional parameter of the HTML. Textbox () method can be used to output Additional HTML attributes. The following code shows how to present additional <input type = text "/> Elements
Size = "30" and class = "mycssclass" attributes. Note: The @ prefix is used to avoid the class conflict of the key words that conflict with C.
<% = Html. Textbox ("title", model. Title, new {size = 30, @ class = "myclass"}) %>
In addition to the above writing method, there are:
<% = Html. textboxfor (M => M. Name) %> and directly write the tag <input id = "username" name = "username" type = "text"/>
Previous Article: entity classes automatically generated by using LINQ in ASP. net mvc 6 models -- (submit form) next article: ASP. net mvc 8 Form Verification