zoukankan      html  css  js  c++  java
  • MVC各种传值方式

    示例一:
    ViewData传值.
    HomeController.cs Code:

    public ActionResult Index()
    {
         ViewData["Title"] = "Home Page";
         ViewData["Message"] = "Welcome to ASP.NET MVC!";
         return View();
    }
    Views/Home/Index.aspx Code:

    <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
        <p>
        <%= Html.Encode(ViewData["Message"]) %>
        </p>
    </asp:Content>

    结果:在页面上显示Welcome to ASP.NET MVC!

    示例二:
    带参数传值.
    URL Routing规则:

    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{param}",                           // URL with parameters
        new { controller = "Home", action = "Index", param = "" }  // Parameter defaults
    );
    HomeController.cs Code:

    public ActionResult Index(string param,int? paraInt,string paraStr)
    {
        ViewData["Title"] = "Home Page";
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        ViewData["Param"] = param;
        ViewData["ParaInt"] = paraInt;
        ViewData["ParaStr"] = paraStr;
        return View();
    }
    Views/Home/Index.aspx Code:

    <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
        <p>
        <%= Html.Encode(ViewData["Message"]) %>
        </p>
        <p>
        <%= Html.Encode(ViewData["Param"]) %>
        </p>
        <p>
        <%= Html.Encode(ViewData["ParaInt"] ?? (object)"(null)")%>
        </p>
        <p>
        <%= Html.Encode(ViewData["ParaStr"] ?? (object)"(null)")%>
        </p>
    </asp:Content>
    结果:
    访问:/home/index/hello?paraint=520&parastr=world
    显示: hello 520 world
    访问:/home/index/hello
    显示:hello (null) (null)

    示例三:
    强类型传值:
    新建一个xml文件:
     
    <?xml version="1.0" encoding="utf-8" ?>
    <root>
      <item name="Sea">
        <animal>Fish</animal>
        <animal>Shrimp</animal>
        <animal>Crab</animal>
      </item>
      <item name="Sky">
        <animal>Bird</animal>
        <animal>Man</animal>
      </item>
    </root>
    新建一个类读取xml数据.
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Xml.Linq;
    using System.Web.Hosting;

    namespace ViewData.Models
    {
        public class Space
        {
            public string Name { getset}
            public string[] Animal { getset}
            private static Space space;
            public IEnumerable<Space> GetSpace()
            {
                XDocument xml = XDocument.Load(HostingEnvironment.MapPath("~/App_Data/Space.xml"));
                IEnumerable<Space> results = from p in xml.Root.Elements("item")
                                             select new Space
                                             {
                                                 Name = p.Attribute("name").Value,
                                                 Animal = p.Elements("animal").Select(r => r.Value).ToArray()
                                             };
                return results;
            }
            public static Space Instance
            {
                get 
                {
                    if (space == null)
                    {
                        space = new Space();
                    }
                    return space;
                }
            }
        }
    }
    在HomeController内添加Action:
     
    public ActionResult About()
    {
        ViewData["Title"] = "About Page";

        return View(Space.Instance.GetSpace());
    }
    在About.aspx.cs后天修改如下.
     
    public partial class About : ViewPage<IEnumerable<Space>>
    About.aspx调用数据:
     
    <%foreach (var p in ViewData.Model){%>
    <ul>
      <li><%=p.Name %>
      <ul>
      <%foreach (string r in p.Animal){ %>
      <li><%=r%></li>
      <%} %>
      </ul>
      </li>
    </ul>
    <%} %>
    结果:
     
     
    示例四:
    表单的值回传给服务器.
    新建一个UserM类:UserM.cs
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace ViewData.Models
    {
        public class UserM
        {
            public string Name { getset}
            public string Password { getset}
            private static UserM user;
            
            public static UserM Instance 
            {
                get 
                {
                    if (user == null)
                    {
                        user = new UserM();
                    }
                    return user;
                }
            }
        }
    }
    HomeControllers添加Action
     
    [AcceptVerbs("GET")] //GET调用
    public ActionResult Form()
    {
        return View(UserM.Instance);
    }

    [ActionName("Form"), AcceptVerbs("POST")]//POST调用
    public ActionResult SaveForm()
    {
        UpdateModel(UserM.Instance, new[] { "Name""Password" });
        return View(UserM.Instance);
    }

    Form.aspx.cs后台添加强类型引用
     
    public partial class Form : ViewPage<UserM>

    Form.aspx
    <p>
    <%=Html.Encode(ViewData.Model.Name) %><br />
    <%=Html.Encode(ViewData.Model.Password) %>
    </p>
        <%Html.BeginForm();%>
        Name:<%=Html.TextBox("Name")%>
        Password:<%=Html.Password("Password") %>
        <input type="submit" value="Submit" />
        <%Html.EndForm(); %>
    结果:在form表单的内容将显示到当前页面
     
     
     
     
     
  • 相关阅读:
    svn 如果遇到an unversioned directory of the same name already exists的解决办法
    记一次keepalived脑裂问题查找
    zabbix3.2部署
    mysql配置文件
    CentOS 6.6 搭建Zabbix 3.0.3 过程
    CDN网络原理
    Vmware ESXi 6.5 安装手册
    Out of resources when opening file ‘./xxx.MYD’ (Errcode: 24)解决方法
    MongoDB主从复制,主主复制
    mysql主从复制跳过错误
  • 原文地址:https://www.cnblogs.com/pengyanfu/p/3296786.html
Copyright © 2011-2022 走看看