Asp.net mvc4 快速入门之构建表单
1、asp.net mvc4 Index.cshtml页面上构建表单form的方式
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } @model MvcFastEntry.Models.Student @*构建form表单方式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> } @*构建form表单方式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>
说明:(1)、@model MvcFastEntry.Models.Student 引用Student.cs实体类
Student.cs类代码如下:
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);} 其中“BiaoDan”是控制器的名称,“Index”是action方法的名称
BiaoDan控制器代码如下:
public class BiaoDanController : Controller { // // GET: /BiaoDan/ //该示例展示怎么构建form表单 public ActionResult Index() { Student student = new Student() { id = 1, userClass = "二班", userName = "小明" }; return View(student); } } }
作者:学习靠自己
出处:http://www.cnblogs.com/net064/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
土豪打赏