.Net MVC之间的关系以及如何运用
.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之间的关系以及如何运用的更多相关文章
- 【整理】JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系
#[整理]JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系 之间的关系
郭晨 软件151 1531610114 [整理]JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系 visio文件下载 概述 一个JavaEE的项 ...
- Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的关系
转载请包含网址:http://blog.csdn.net/pathuang68/article/details/7351317 一.Surface Surface就是“表面”的意思.在SDK的文档中, ...
- Android 展示控件之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的关系
一.Surface Surface在SDK的文档中的描述是这样的:Handle onto a raw buffer that is being managed by the screen compos ...
- 转:spring data jpa、 hibernate、 jpa 三者之间的关系
原文链接:spring data jpa. hibernate. jpa 三者之间的关系 spring data jpa hibernate jpa 三者之间的关系 JPA规范与ORM框架之间的关系是 ...
- java设计模式、框架、架构、平台之间的关系
设计模式<框架<架构<平台,从复用角度讲,设计模式是代码级复用.框架是模块级复用.架构是系统级复用.平台是企业应用级复用. 1.设计模式 为什么要先说设计模式?因为设计模式在 ...
- ASP.NET-MVC中Entity和Model之间的关系
Entity 与 Model之间的关系图 ViewModel类是MVC中与浏览器交互的,Entity是后台与数据库交互的,这两者可以在MVC中的model类中转换 MVC基础框架 来自为知笔记(Wiz ...
- .NET Core与.NET Framework、Mono之间的关系
随着微软的.NET开源的推进,现在在.NET的实现上有了三个.NET Framework,Mono和.NET Core.经常被问起Mono的稳定性怎么样,后续Mono的前景如何,要回答这个问题就需要搞 ...
- .NET Core 和 .NET Framework 之间的关系
引用一段描述:Understanding the relationship between .NET Core and the .NET Framework. .NET Core and the .N ...
随机推荐
- SOJ 2785_Binary Partitions
[题意]将一个数用二进制数表示,求一共有多少种表示方法. [分析]思路一:完全背包 [代码] #include <iostream> #include <cstdio> #in ...
- Network -UVa315(连通图求割点)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=sh ...
- hdu4701 Game(递推博弈)
题意: Alice初始有A元,Bob有B元. 有N个物品,第i个物品价值为Ci.Alice和Bob轮流买一些(>=1)物品.不能移动的人输.购买有一个限制,对于第1 个之后物品,只有当第i-1个 ...
- easyUi 学习笔记 (一) 使用easyui 和ztree 创建前端框架
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- python操作mysql方法和常见问题
http://www.cnblogs.com/ma6174/archive/2013/02/21/2920126.html 安装mysql模块 sudo easy_install mysql-pyth ...
- Mysql入门实战中
前面一章主要解说了mysql的入门学习.包括数据库,表的管理,以及对数据的增删改,本章主要介绍mysql最重要的语句select的使用方法.将select的大部分使用方法进行分别解说. 全部代码下载( ...
- angular 的ui.router 定义不同的state 对应相同的url
Angular UI Router: Different states with same URL? The landing page of my app has two states: home-p ...
- day1--大数据概念,hadoop介绍,hdfs整体运行机制
1.什么是大数据 基本概念 在互联网技术发展到现今阶段,大量日常.工作等事务产生的数据都已经信息化,人类产生的数据量相比以前有了爆炸式的增长,以前的传统的数据处理技术已经无法胜任,需求催生技术,一套用 ...
- Effective C++ Item 41 了解隐式接口和编译期多态
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:class 和 templates 都支持接口和多态. 对 classes 而言接口是 ...
- 字节序:Big Endian 和 Little Endian
一.字节序 字节序,也就是字节的顺序,指的是多字节的数据在内存中的存放顺序. 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.例如:如果C/C++中的一个int型变量 a 的起始地址是& ...