学了很久才知道如何使用mvc 模型,model加controller 加view,以前只知道叫啥,等自己真正了解asp.net 之后才会使用这个模型,
先上一段代码呗,
视图层View :
<!-根据自己的项目建立在哪个文件里面,myproject 是我的项目名字”-->
@model myproject.Models.LoginModel
@{
Layout = null;
}
<!--如果是创建强类型的视图并选择了LoginModel,上面的代码则由vs自动生成,仅仅是一段代码,手写的效果是一样的,所以一般情况我不会勾选“强类型视图”-->
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>用户登陆</title>
</head>
<body>
<!--Html.BeginForm()等同于在html代码里写<form>.....</form>是一样的-->
@using (Html.BeginForm())
{
<div>
用户名:
</div>
<div>
<!--创建用户名文本框,等同于<input type="text" />-->
@Html.TextBoxFor(model => model.UserName)
</div>
<div>
密码:
</div>
<div>
<!--创建用户名文本框,等同于<input type="password" />-->
@Html.PasswordFor(model => model.Password)
</div>
<div>
<input type="submit" value="登录" />
</div>
}
</body>
</html>
后面就是Model层(Model 层很简单,就是用来接收页面的参数,把接收过后的值传给控制器)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace myproject.Models
{
//以页面为主的用户登录模型
public class LoginModel {
public string UserName { get; set; }
public string Password { get; set; }
}
}
控制器层Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace myproject.Controllers
{
public class UserController : Controller
{
//登陆控制器
public ActionResult Login()
{
return View();
}
[HttpPost]//登陆控制器
public ActionResult Login(Models.LoginModel loginModel)
{
if (loginModel.UserName == "张三" && loginModel.Password == "123456")
Response.Write("正确!");
else
Response.Write("不正确!");
return View();
}
}
}
也可以运行起来,爽歪歪
这里可以给大家分享一个链接 很适合入门http://www.cnblogs.com/iamlilinfeng/archive/2013/04/01/2992432.html