zoukankan      html  css  js  c++  java
  • .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("控制器动作");

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

  • 相关阅读:
    Echarts之内嵌圆环图v5.1.2
    VueX+VueRouter+Cookie实现简单登录页
    命令行安装MySQL
    mysql常用语句
    网际互联及OSI七层模型:
    阿里ICON图标,使用教程
    前端开发小技巧整理
    SQL语句
    web笔记
    jQuery
  • 原文地址:https://www.cnblogs.com/cjdonet/p/6109761.html
Copyright © 2011-2022 走看看