.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. 程序猿Web面试之JSON

     JSON是什么? JSON(JavaScript对象表示法), 是在网络通信下.经常使用的一种数据表达格式,它有助于我们于一个自描写叙述的,独立的和轻的方式呈现并交换数据. 这些数据能够易于和转 ...

  2. WebLogic(12C)——简单公布和JDBC

    一,简单公布应用 1.点击"安装".開始部署应用: 2,找到要部署的项目路径: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFue ...

  3. C#.NET如何判断是否有缺少的using

    调试的时候会报错,红色的波浪线表示出错的位置,右击即可找到对应的using      

  4. SQLite数据库基本操作

    SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使用方便.结构紧凑.高效.可靠. 与其他数据库管理系统不同,SQLite 的安装和运行非 ...

  5. Android 布局自适应屏幕

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenFzNjI3NjExMzA=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  6. Node.js创建自签名的HTTPS服务器

    https://cnodejs.org/topic/54745ac22804a0997d38b32d  用Node.js创建自签名的HTTPS服务器  发布于 4 年前  作者 eeandrew  6 ...

  7. c# WinForm的一些问题

    工作中,用WinForm写了一段程序,刚开始运行正常,后来替换为公司框架的时候,发现原来用Label拼的表格控件,里面的Text无法显示,后来发现,父控件的ForColor为Control导致,子空间 ...

  8. [Codeforces 639B] Bear and Forgotten Tree 3

    [题目链接] https://codeforces.com/problemset/problem/639/B [算法] 当d > n - 1或h > n - 1时 , 无解 当2h < ...

  9. bzoj3670 [Noi2014]动物园——KMP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3670 第一次写KMP算法...又T又WA了半天... 1. num 数组表示包括其本身的前缀 ...

  10. android View页面布局总结 最全总结(转)

    下面是我在工作中总结的内容,希望对大家有帮助. 一.布局 View的几种布局显示方式有下面几种:线性布局(LinearLayout).相对布局(RelativeLayout).表格布局(TableLa ...