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