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();
            }
        }
    }
  • 相关阅读:
    gitlab
    MySQL千万级别大表,你要如何优化?
    kafka入门
    zookeeper的原理和应用
    MySQL 性能优化之慢查询
    Redis一些新的看法
    mysql 数据库锁
    MYSQL查看进程和kill进程
    hadoop批量命令脚本xcall.sh及jps找不到命令解决
    java stream 处理分组后取每组最大
  • 原文地址:https://www.cnblogs.com/pnljs/p/3735026.html
Copyright © 2011-2022 走看看