zoukankan      html  css  js  c++  java
  • 表单的提交

    对于表单的提交:

    用惯WebForm的可能会忘掉了传统的表单提交方式,jsp,asp,php都是这样。MVC是返璞归真了。
    用到的Model

    首先:我们首先在view层新建试图,命名为login

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>login</title>
    </head>
    <body>
        <div>
        <%using (Html.BeginForm("chklogin", "Home"))
          {%>
        <ul>
        <li>用户登陆</li>
        <li><input type="text" name="username" id="username" />
        </li>
         <li><input type="text" name="password" id="password" />
        </li>
        <li><input type="submit" name="sub" id="sub" value="登陆" onclick="return sub_onclick()" /></li>
        </ul>
        <%} %>
        </div>
    </body>
    </html>

    然后在model层新建实体user

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace NerdDinnerTest.Models
    {
        [Serializable]
        public class User
        {
            public string userName
            {
                get;
                set;
            }
            public string password
            {
                get;
                set;
            }
        }
    }
    
    然后在控制层controller里新建对应的HomeController

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Text;
    
    namespace NerdDinnerTest.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Home/
    
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult login()
            {
                return View();
            }
    
            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult chklogin(FormCollection form)
            {
                NerdDinnerTest.Models.User user = new Models.User();
                user.userName = form["username"];
                user.password = form["password"];
                if (checkuser(user))
                {
                    Session["user"] = user;
                    return RedirectToAction("Main", "User");
                }
                else
                    return RedirectToAction("login");
    
            }
    
            bool checkuser(Models.User user)
            {
                if (string.IsNullOrEmpty(user.password) || string.IsNullOrEmpty(user.password))
                {
                    return false;
                }
                else
                {
                    return (user.userName.ToLower() == "admin" && user.password.ToLower() == "admin");
                }
            }
    
        }
    }
    
    

    忘了,要在global里设置router

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace NerdDinnerTest
    {
        // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
        // 请访问 http://go.microsoft.com/?LinkId=9394801
    
        public class MvcApplication : System.Web.HttpApplication
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.MapRoute(
                   "Default",
                   "",
                   new { controller = "Home", action = "Index" }
                   );
                routes.MapRoute(
                    "Default1",
                    "{action}.htm",
                    new { controller="Home",action="Index"}
                    );
                routes.MapRoute(
                   "User",
                   "User/{action}.htm",
                   new { controller = "User", action = "Main" }
                   );
    
            }
    
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                RegisterRoutes(RouteTable.Routes);
            }
        }
    }
    

    这样一个非常简单的表单提交做成了。

  • 相关阅读:
    Cesium中监听MOUSE_MOVE事件获取经纬度和高度
    CentOS系统重命名
    docker安装步骤
    nginx发布vue 项目
    解决git 本地代码与远程仓库冲突问题
    js通过className删除元素
    bootstrap treeview基本运用
    自定义组件模拟v-model
    使用a标签下载**.txt文件, 而不是直接打开
    mongoose 开源http库
  • 原文地址:https://www.cnblogs.com/linjiancun/p/1829119.html
Copyright © 2011-2022 走看看