1. The way to build a form on the asp.net mvc4 Index.cshtml page
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } @model MvcFastEntry.Models.Student @ *Construct form form method 1* @ @using(Html.BeginForm("Index","BiaoDan",FormMethod.Post)) { <div> @Html.LabelFor(m=>m.userName)</div> <div> @Html.TextBoxFor(m => m.userName)</div> <div> <input type="submit" value="提交" /></div> } @ *Construct form form method 2* @ <div> @{Html.BeginForm( " Index " , " BiaoDan " , FormMethod.Post);} <div> @Html.LabelFor(m=>m.userName)</div > <div> @Html.TextBoxFor(m => m.userName)</div> <div> <input type="submit" value="提交" /></div> @{Html.EndForm();} </div>
Description: (1), @model MvcFastEntry.Models.Student refers to the Student.cs entity class
The Student.cs class code is as follows:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace MvcFastEntry.Models { public class Student { public int id { get; set; } [Display(Name="姓名")] public string userName { get; set; } public string userClass { get; set; } } }
(2), @{Html.BeginForm( " Index ", " BiaoDan " , FormMethod.Post);} where "BiaoDan" is the name of the controller and "Index" is the name of the action method
The BiaoDan controller code is as follows:
public class BiaoDanController : Controller { // // GET: /BiaoDan/ // This example shows how to build a form form public ActionResult Index() { Student student = new Student() { id = 1, userClass = " Second Class " , userName = "小明" }; return View(student); } } }