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();
            }
        }
    }
  • 相关阅读:
    日期和时间
    怎样将GB2312编码的字符串转换为ISO-8859-1编码的字符串?
    数据类型之间的转换:
    类的加载顺序,支出下列程序的输出结果
    内部类
    对象的类型转换
    简单继承
    封装
    计算a+b
    U盘删除文件时提示“文件或目录损坏且无法读取”的解决方法
  • 原文地址:https://www.cnblogs.com/pnljs/p/3735026.html
Copyright © 2011-2022 走看看