MVC表单提交写法1

2020-12-13 04:56

阅读:542

标签:style   blog   class   code   c   java   

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

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

Model:

soscw.com,搜素材
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; }
    }
}
soscw.com,搜素材

View:

soscw.com,搜素材
@model MvcApplication2.Models.Student
@{
    ViewBag.Title = "Index";
}

Index

@using (Html.BeginForm("Index", "Home")) { @Html.Label("label1") @Html.TextBoxFor(m=>m.Name) "submit" value="提交1" /> }
soscw.com,搜素材

Controller:

soscw.com,搜素材
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();
        }
    }
}
soscw.com,搜素材

 

MVC表单提交写法1,搜素材,soscw.com

MVC表单提交写法1

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/pnljs/p/3735026.html


评论


亲,登录后才可以留言!