I just started learning MVC, and it was a bit difficult to switch from Webform. Many things have already changed to another form, and I am constantly adapting to this. Let’s explain a simple registration page. Let’s get familiar with the commonly used operations in MVC.
The User class in Model is as follows
<span style="font-family:SimSun;font-size:18px;">public class User { //用号登录姓名 public string LoginName { get; set; } //用户密码 public string Password { get; set; } //用户的电子邮件 public string Email { get; set; } //用户的手机号码 public string Phone { get; set; } }</span>
The biggest benefit of MVC is separation of concerns, which means we are developing In the process, you don’t need to worry about what the page is. You can develop business logic first. The following are the operations in RegisterController
<span style="font-family:SimSun;font-size:18px;">public class registerController : Controller { //注册完的信息显示 public ActionResult Create(FormCollection form) { User user = new User() { //两种获取方法,一种通过request,一种通过formcollection类 LoginName=form["name"], // LoginName = Request.Form["loginName"], Password = Request.Form["password"], Phone = Request.Form["phone"], Email = Request.Form["email"] }; //通过viewdata向界面传递值 ViewData["UserInfo"] = user; return View(); } //注册的方法 public ActionResult Reg() { //字典类型的键值对 IDictionary<int, string> star = new Dictionary<int, string>(); //添加值 star.Add(1, "白羊座"); star.Add(2, "金牛座"); star.Add(3, "双子座"); star.Add(4, "巨蟹座"); star.Add(5, "狮子座"); star.Add(6, "处女座"); star.Add(7, "天秤座"); star.Add(8, "天蝎座"); star.Add(9, "射手座"); star.Add(10, "摩羯座"); star.Add(11, "水瓶座"); star.Add(12, "双鱼座"); //为列表赋值 SelectList starList = new SelectList(star, "key", "value"); //向界面传递值 ViewData["star"] = starList; return View(); } }</span>
With the above business logic, we can implement it The page is operated.
Registration page
<span style="font-family:SimSun;font-size:18px;"><%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><!DOCTYPE html><html><head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Reg</title></head><body> <div id="register"><% using (Html.BeginForm("Create", "Register", FormMethod.Post)) { %> <table border="0" width="500"> <thead><tr><td colspan="2"> <h3 id="用户注册">用户注册</h3> </td></tr></thead><tbody> <tr><td class="td_left">登录名:</td> <td><%=Html.TextBox("loginName") %></td> </tr><tr><td class="td_left">密码:</td> <td><%=Html.Password("password") %></td> </tr><tr><td class="td_left">确认密码:</td> <td><%=Html.Password("password2") %></td> </tr><tr><td class="td_left">星座:</td> <td><%=Html.DropDownList("star") %></td> </tr><tr><td class="td_left">性别:</td> <td><%=Html.RadioButton("sex", true, new { style = "border:0; width:30px;" })%>男 <%=Html.RadioButton("sex", false, new { style = "border:0; width:30px;" })%>女</td> </tr><tr><td class="td_left">已婚:</td> <td><%=Html.CheckBox("married", false, new { style = "border:0; width:30px;" })%></td> </tr><tr><td class="td_left">安全邮箱:</td> <td><%=Html.TextBox("email") %>输入邮箱</td> </tr><tr><td class="td_left">联系电话:</td> <td><%=Html.TextBox("phone") %>输入联系电话</td> </tr><tr> <td colspan="2" align="center"><br /><button type="submit"> 提交 </button><br /><br /></td> </tr> </tbody> </table><%} %></div></body></html></span>
The detailed information page is as follows
<span style="font-family:SimSun;font-size:18px;"><%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><!DOCTYPE html><html><head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Create</title></head><body> <div id="register"> <h2 id="用户注册">用户注册</h2> <% loginMvc.Models.User user = ViewData["UserInfo"] as loginMvc.Models.User; %> 你刚才提交的信息如下:<br /> 登 录 名:<%= user.LoginName %><br /> 登录密码:<%= "******" %><br /> 安全邮箱:<%= user.Email %><br /> 联系电话:<%= user.Phone %><br /> <br /> <button>确定注册</button> <a href="/Register">返回重填</a> <br /><br /></div></body></html></span>
Case Analysis
Several key points are used in the above example. Let’s analyze it below.
1. Get the passed value
In the above example, we use two methods to get the passed value, namely the FormCollection class and the Request object. These two usages are the same.
2. Page jump
The BeginForm operation of the HtmlHelper class is used, such as Html.BeginForm("Create", "Register", FormMethod.Post), which represents the post method as the page. When submitting, pass it to the Create method in the Register controller. There are many parameters of this method, you can study them yourself.
3. Viewdata returns a value to the page. Viewdata is like a container that can load anything. In the above example, it is passed through viewdata. For example, in the controller ViewData["UserInfo"] = user; loads the user object into the container, and then uses login.models.user user= ViewData["UserInfo"] as loginMvc.Models.User on the page to user assignment, it should be noted that forced conversion is required here.
4. The difference between viewdata and TempData
The ViewData property is a ViewDataDictionary class that can be used to store the data type of any object, but the stored key value must be a string. ViewData has a feature, which is It will only exist in the current HTTP request, and unlike the session, the data can be carried to the next HTTP request. Like ViewData, both belong to the dictionary class, but the data in it is just a request for a web page
??

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.