zoukankan      html  css  js  c++  java
  • Asp.Net MVC之 自动装配、动态路径(链接)等

    一、Model层

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 
     6 namespace Mvc_Demo.Models
     7 {
     8     public class Person
     9     {
    10         public int Age { get; set; }
    11         public string  Name { get; set; }
    12         public string  Sex { get; set; }
    13     }
    14 }

    二、控制器层

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Mvc;
     6 using Mvc_Demo.Models;
     7 
     8 namespace Mvc_Demo.Controllers
     9 {
    10     public class PersonController : Controller
    11     {
    12         //==================自定义方式的自动装配方法=========================
    13         // GET: /Person/
    14         //1、通过默认的get请求方式,执行这个行为,请求到AddPerson这个view页面,其才会展示出来
    15         [HttpGet]//默认请求方式
    16         public ActionResult AddPerson()
    17         {        
    18             return View();
    19         }
    20         //2、在AddPerson这个view页面输入值以后,点击提交,因为指定了他们的提交方式为post方式,
    21         //故提交以后,会执行此行为,然后显示AddPerson1页面,将数据进行展示出来
    22         [HttpPost]
    23         public ActionResult AddPerson(Person person) 
    24         {
    25             ViewData.Model = person;
    26             return View("AddPerson1");
    27         }
    28         //===============================单个值的自定义装配============================
    29         [HttpPost]
    30         public ActionResult Purple(string dd)
    31         {
    32             ViewBag.DD = dd;
    33             return View("Purple");
    34         }
    35     }
    36 }
    View Code

    三、视图(View)

    1.处理视图

     1 @*引入命名空间*@
     2 @using Mvc_Demo.Controllers;
     3 @using Mvc_Demo.Models;
     4 @model Mvc_Demo.Models.Person
     5 
     6 @{
     7     ViewBag.Title = "AddPerson";
     8 }
     9 @* ========================自定义方式的自动装配================================= *@
    10 <h2>AddPerson</h2>
    11 @using (Html.BeginForm("AddPerson", "Person", FormMethod.Post))
    12 {
    13     <span>年龄:</span>
    14     @Html.TextBoxFor(p=>p.Age)
    15     <hr />
    16     <span>姓名:</span>
    17     @Html.TextBoxFor(p=>p.Name)
    18     <hr />
    19     <span>性别:</span>
    20     @Html.TextBoxFor(p=>p.Sex)
    21     <hr />
    22     <input type="submit" name="Submit" value="提交" />
    23 }
    24 @* ========================单个值的自动装配================================ *@
    25 @using (Html.BeginForm("Purple", "Person", FormMethod.Post))
    26 {
    27     @Html.TextBox("dd")
    28     <input type="submit" name="name" value="提交哈" />
    29 }
    30 @* ===========================跳转链接============================== *@
    31 @*链接跳转失败*@
    32 <a href="@Url.Action("Purple", "Person")">跳转到AddPerson1页面</a>
    33 <hr />
    34 @Html.ActionLink("跳转到AddPerson1页面", "AddPerson1", "Person")
    35 <hr />
    36 @* ====================在指定位置输出值===================================== *@
    37 @*在指定位置输出值*@
    38 @Html.Raw("我是HTML帮助类方式输出")
    39 @*在页面最顶端输出值*@
    40 @{
    41     Response.Write("我是Response方式输出");
    42 }

    2.显示视图(1)

     1 @using Mvc_Demo.Controllers;
     2 @model Mvc_Demo.Models.Person
     3 
     4 @{
     5     ViewBag.Title = "AddPerson1";
     6 }
     7 @* ========================自定义方式的自动装配================================= *@
     8 <h2>AddPerson1</h2>
     9 <span>年龄:</span>
    10 <h1>@Model.Age</h1>
    11 <hr />  
    12 <span>姓名:</span>
    13 <h1>@Model.Name</h1>
    14 <hr />  
    15 <span>性别:</span>
    16 <h1>@Model.Sex</h1>
    17 <hr />
    18 
    19 
    20 
    21 
    22 @*当路由是通过动态规则方式动态生成时,通过这种方式会动态生成路径*@
    23 @Html.ActionLink("跳转到AddPerson页面", "AddPerson", "Person")

    3.显示视图(2)

     1 @model Mvc_Demo.Models.Person
     2 
     3 @{
     4     ViewBag.Title = "Purple";
     5 }
     6 
     7 <h2>Purple</h2>
     8 @* ========================单个值的自动装配================================ *@
     9 @{
    10     //string str=Convert.ToString(@ViewBag.DD);
    11     @*@Html.TextArea(str)*@
    12     @ViewBag.DD
    13 }
  • 相关阅读:
    mvn
    MySQL 数据类型
    Request获取客户端IP
    struts1
    tomcat8.5 Host-Manager配置访问的方法
    struts2框架
    windows注册表
    windows常用命令
    浏览器内核
    创建分区swap分区
  • 原文地址:https://www.cnblogs.com/pang951189/p/7792469.html
Copyright © 2011-2022 走看看