zoukankan      html  css  js  c++  java
  • 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);
            }
    }
    }
  • 相关阅读:
    解决VS2012新建MVC4等项目时,收到加载程序集“NuGet.VisualStudio.Interop…”的错误
    BOM 清除
    sleep和Sleep区别
    出现error: stray ‘357’ in program的根源
    vi 复制或剪切多行超级强大方法
    CentOS乱码解决方法
    VBS实现文本文件按行数拆分的脚本
    python-门户应用状态检查脚本
    HTA+VBS实现的话单核查小工具
    oracle 11g 安装
  • 原文地址:https://www.cnblogs.com/net064/p/8876633.html
Copyright © 2011-2022 走看看