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; }
    }
}
View Code

(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);
        }
}
}
原文地址:https://www.cnblogs.com/net064/p/8876633.html