zoukankan      html  css  js  c++  java
  • MVC表单提交写法1

    初学MVC,感觉跟以前的aspx页面差别很大,我们就先来看看MVC的表单是怎么提交的吧。

    现在我们用一个最简单的例子来看一看MVC是怎么提交表单的(这一个例子中,我们的关注点是如何提交表单,所以不涉及到任何的业务逻辑)

    Model:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MvcApplication2.Models
    {
        public class Student
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }

    View:

    @model MvcApplication2.Models.Student
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    @using (Html.BeginForm("Index", "Home"))
    {
        @Html.Label("label1")
        @Html.TextBoxFor(m=>m.Name)
        
        <input type="submit" value="提交1" />
    }

    Controller:

    using MvcApplication2.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MvcApplication2.Controllers
    {
        public class HomeController : Controller
        {
            //页面上的 @using (Html.BeginForm("Index", "Home")) 表明我们表单是  Home/Index  
            public ActionResult Index()
            {
                return View();
            }
    
            //标记成HttpPost表示当点界面上的"提交1"时,会调用此方法。
            //即实现了,从界面到后台的一个过程。
            [HttpPost]
            public ActionResult Index(Student student)
            {
                return View();
            }
        }
    }
  • 相关阅读:
    PHP序列化和反序列化
    移动端纯css超出盒子出现横向滚动条
    css3盒子flex
    css怎么设置2个div同行,第一个固定宽度,第二个占满剩余的部分
    PHP对象基础
    常用header头
    【转载】文件上传那些事儿,文件ajax无刷上传
    简单工厂模式(Simple Factory Pattern)
    单例模式(singleton)
    UML类图
  • 原文地址:https://www.cnblogs.com/pnljs/p/3735026.html
Copyright © 2011-2022 走看看