.Net MVC组成部分:视图(views)模型(model)控制器(controller)以及路由(RouteConfig),视图跟模型的数据不进行直接的交互,他们是通过控制器进行视图模型之间的数据交互,现在我们来一步步讲讲视图如何跟模型之间交互。

视图:用来显示客户端界面

模型:用来数据之间的交互

控制器:用来将视图跟模型关联起来

路由:用来配置URL

推荐几篇入门的文章,MVC HtmlHelper用法大全

路由器设置默认加载页面:

路由:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "控制器名称", action = "控制器动作", id = UrlParameter.Optional }
            );
        }

如何将视图模型控制器关联起来

方法一:

创建视图时,选择对应的模型,去创建。

视图:

@Html.TextBoxFor(model => model.tel, new { @class = "user_input", tabindex = "1" })

在视图中创建,在模型中创建一个字段

模型:

public string tel { get; set; }

在控制器中实例化模型,然后调用模型里面的字段名称,将字段名称赋值,返回字段名,就会产生字段的内容。比如说:

public ActionResult Login(Field field, string textyzm)
        {
            string tel = field.tel;
            return View();
        }

方法二:

视图:

@Html.TextBox("texxname", "", new { })

在对应的动作中传递参数,注:参数名要与跟视图创建的名称相同。

控制器:

public ActionResult Index(string textname,)
        {

string ss=textname
            return Content(ss);
        }

提交表单操作

定义一个post请求提交,在控制器里面定义相应的动作,实现表单提交功能。

MVC视图:

<form id="user_form_0" class="user_form" method="post" target="pass_reg_iframe_0" action="#">
            <p>
                <label id="label_username" for="username">用户名</label>
                @Html.TextBoxFor(model => model.name, new { @class = "user_input", tabindex = "1" })
            </p>

<p>
                <label id="label_password_0" for="password_0">密码</label>
                @Html.PasswordFor(model => model.pass, new { @class = "user_input", tabindex = "2" })
            </p>
            <p>
                <label id="label_sex" for="sex">性别</label>
                @Html.DropDownListFor(model => model.sex, new SelectList(ViewBag.hard_value, "value", "text"), new { })
            </p>

<p>
                <label id="label_password_1" for="password_1">确认密码</label>
                @Html.PasswordFor(model => model.qrpass, new { @class = "user_input", tabindex = "3" })
            </p>

<p>
                <label id="label_phone" for="phone">电话</label>
                @Html.TextBoxFor(model => model.tel, new { @class = "user_input", tabindex = "4" })
            </p>

<p>
                <label id="label_mail" for="mail">邮箱</label>
                @Html.TextBoxFor(model => model.email, new { @class = "user_input", tabindex = "4" })
            </p>

<p class="user_p_verifycode">
                <label id="label_verifycode" for="verifycode">验证码</label>
               
<img id="yzm" name="yzm" onclick="CodeChange()" title="看不清?"
src="/Login/GetValidatorGraphics" tabindex="5" />
            </p>
           
<p class="user_p_img_verifycode">@Html.TextBox("textyzm", "", new
{ @class = "user_verifycode", id = "pass_reg_img_verifycode_0", alt =
"验证码图片", title = "验证码图片" })</p>
            <div class="clear"></div>
            <p>
               
<input name="提交" class="user_submit" type="submit" id="user_submit"
style="margin-left:152px;" tabindex="6" value="注册">
            </p>
        </form>

MVC控制器:

[HttpPost]
        public ActionResult Index(Field fiel, string textyzm)
        {
            //写验证判断验证成功后跳转至index页面
            //字段验证
            bool tpyz = boolyzmyz(textyzm);
            if (tpyz == true)
            {
                return View("Index");
            }
            else
            {
                return View();
            }
        }

MVC跳转

在视图这种跳转:

<a href="/控制器/控制器动作"></a>

在控制器里面跳转:

return View("控制器动作");

纯手打,转载请标注原文出处。

.Net MVC之间的关系以及如何运用的更多相关文章

  1. 【整理】JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系

    #[整理]JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系 ![关系图解](http://images.cnitblog.com/blog/84 ...

  2. JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系

    郭晨 软件151 1531610114 [整理]JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系 visio文件下载 概述 一个JavaEE的项 ...

  3. Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的关系

    转载请包含网址:http://blog.csdn.net/pathuang68/article/details/7351317 一.Surface Surface就是“表面”的意思.在SDK的文档中, ...

  4. Android 展示控件之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的关系

    一.Surface Surface在SDK的文档中的描述是这样的:Handle onto a raw buffer that is being managed by the screen compos ...

  5. 转:spring data jpa、 hibernate、 jpa 三者之间的关系

    原文链接:spring data jpa. hibernate. jpa 三者之间的关系 spring data jpa hibernate jpa 三者之间的关系 JPA规范与ORM框架之间的关系是 ...

  6. java设计模式、框架、架构、平台之间的关系

        设计模式<框架<架构<平台,从复用角度讲,设计模式是代码级复用.框架是模块级复用.架构是系统级复用.平台是企业应用级复用. 1.设计模式 为什么要先说设计模式?因为设计模式在 ...

  7. ASP.NET-MVC中Entity和Model之间的关系

    Entity 与 Model之间的关系图 ViewModel类是MVC中与浏览器交互的,Entity是后台与数据库交互的,这两者可以在MVC中的model类中转换 MVC基础框架 来自为知笔记(Wiz ...

  8. .NET Core与.NET Framework、Mono之间的关系

    随着微软的.NET开源的推进,现在在.NET的实现上有了三个.NET Framework,Mono和.NET Core.经常被问起Mono的稳定性怎么样,后续Mono的前景如何,要回答这个问题就需要搞 ...

  9. .NET Core 和 .NET Framework 之间的关系

    引用一段描述:Understanding the relationship between .NET Core and the .NET Framework. .NET Core and the .N ...

随机推荐

  1. 04-js的运算符

    <html> <head> <title>js的运算符学习</title> <meta charset="UTF-8"/> ...

  2. MongoDB小结06 - update【$push】

    数组修改器,既然名字都这样叫了,那么这个修改器就只能对数组进行操作啦. db.user.update({"name":"qianjiahao"},{" ...

  3. linux复制文件/移动文件从一个文件夹到另一个文件夹

    cp ~/baidu/* ~/zhidao/cp -r逐层复制mv /data/link /usr/local 移动文件夹 解决方法 1.在cp命令后,加一个选项 -r . cp -r  /TEST/ ...

  4. maven bug之Maven:Non-resolvable parent POM: Failure to find错误

    使用Maven编译淘宝的TimeTunnel项目时遇到如下错误: [INFO] Scanning for projects...[ERROR] The build could not read 1 p ...

  5. ubuntu Install Firefox

    Firefox 下载文件以.tar和.bz2格式保存,必须从这些压缩包中提取文件.不想删除当前安装的 Firefox,给每个版本的 Firefox 创建一个单独的文件夹. 例如:1.Firefox 版 ...

  6. react 项目实战(一)创建项目 及 服务端搭建

    1.安装 React社区提供了众多的脚手架,这里我们使用官方推荐的create-react-app. //安装脚手架 npm install -g create-react-app //生成并运行项目 ...

  7. 10.11无法打开Xcode6.4的解决方法

    前言 mac升级到10.11版本号并安装Xcode7.0Beta之后,Dock中的Xcode6.3图标上出现一个禁止符号,打开提示到App store更新最新版本号,更新到6.4之后问题依然,还是提示 ...

  8. Codeforces Beta Round #2 B. The least round way

    这个2B题还好~~ 题目大意: 给出一个矩阵.从左上走到右下,仅仅能往右或下走.路径中每一个格子有一个数.这些数相乘得出一个数. 求这个数末尾零最少的一条路径. 解题思路: 找出一条路径.乘积得数中素 ...

  9. python开发【第4篇】【进程、线程、协程】

    一.进程与线程概述: 进程,是并发执行的程序在执行过程中分配和管理资源的基本单位,每一个进程都有一个自己的地址空 间. 线程,是进程的一部分,一个没有线程的进程可以被看作是单线程的.线程有时又被称为轻 ...

  10. hadoop分布式安装部署具体视频教程(网盘附配好环境的CentOS虚拟机文件/hadoop配置文件)

    參考资源下载:http://pan.baidu.com/s/1ntwUij3视频安装教程:hadoop安装.flvVirtualBox虚拟机:hadoop.part1-part5.rarhadoop文 ...