经验之谈 ---- ASP.NET应用程序MVC5模式下的简单实例项目

 刚开始做ASP.NET应用程序的时候,自己一脸蒙B,具体的程序流程都不懂,所以自己打算写一个最简单的项目来看看ASP.NET MVC项目的具体流程。

若有写得不好的,还望指出.吐舌头

目录结构如下图所示:

 

在企业中开发的时候需要自建Area(领域),所以我把最初项目带有的MVC文件夹我都删除了。

 

Controllers(控制层) UserController.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication4.Areas.LZX.Models;
namespace WebApplication4.Areas.LZX.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /LZX/User/
        public ActionResult Index()
        {
            return View();
        }
        //操作名(方法名)必須与Views/User 下的cshtml文件的名字一致,当return View(),就会跳转到相对应的视图
        public ActionResult LoginSuccess()
        {
            return View();
        }
       [HttpPost]
        public ActionResult Login(String UserName, String Password)
        {

            User user = new User();
            user.UserName = UserName;
            user.Password = Password;
         //可以写具体的逻辑代码,比如登录验证....
            return Json(user);
        }
	}
}//可以写具体的逻辑代码,比如登录验证....
            return Json(user);
        }
	}
}

 

Models(模型层) User.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication4.Areas.LZX.Models
{
    public class User
    {
        public String UserName { get; set; }
        public String Password { get; set; }
    }
}

 

Views(视图层) User/Index.cshtml代码:

@{
    ViewBag.Title = "Index";
}
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<h2>登錄界面:</h2>
<form method="post">
    帳號:<input type="text" id="username" name="username" /><br /><br />
    密碼:<input type="password" id="password" name="password" /><br /><br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="button" id="submit" value="登錄" /><br />
</form>
<script>
    $(function () {
        $("#submit").click(function () {
   
            var UserName = $("#username").val();
            var Password = $("#password").val();
            $.ajax({
                type: "post",
                url:"/LZX/User/Login",
                data: { UserName: UserName, Password: Password },
                dataType:"json",
                async:false,
                success:function(data){
                alert(data.UserName);
                window.location.href = "/LZX/User/LoginSuccess?username="+data.UserName;              
                }
            });
        });
    });
</script>

 

Views(视图层) User/LoginSuccess.cshtml代码:

@{
    ViewBag.Title = "LoginSuccess";
}

<h2>@Request["username"] LoginSuccess 登录成功。。。。</h2>

 

运行结果:

項目源码下載地址(代码会有点小区别,同样的原理):https://download.csdn.net/download/laizhixue/11010861

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

laizhixue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值