zoukankan      html  css  js  c++  java
  • MVC入门学习笔记(八)

    七.MVC中的QueryString传值

        MVC中的QueryString传值和普通传值方式是一样的,它同样需要再代码逻辑中获取字符串的值,并在页面中显示,以往asp.net是在.cs文件中获取字符串的值,然后再页面.aspx中进行显示如下:

       代码: Default.aspx

     <%@ Page Title="主页" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
     CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <a  href ="About.aspx?id=12345">hello</a>
        </asp:Content>

     

               About.aspx

    <%@ Page Title="关于我们" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
        CodeBehind="About.aspx.cs" Inherits="WebApplication1.About" %>

    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    </asp:Content>

              About.aspx。cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication1
    {
        public partial class About : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Label1.Text = Request.QueryString["id"];
            }
        }
    }


    在MVC中的传值思想也是这样的

        1.相对于上面,我们在Index.aspx中写入一个QueryString字段,其中new{word="Qstring"}就是定义的字段(注意:只能用word关键字)

       <%=Html.ActionLink("链接", "about", "home",

               new { word = "Qstring" },

               new { @Class="x"}  

        )%>

         2.在控制器HomeController.cs中写入获取字符串,需要再About()方法中获取Index.aspx传递过来的字段

        public ActionResult About()
            {
                ViewData["w"] = Request.QueryString["word"];
                return View();
            }

         3.在About.aspx中接收ViewData[]字典

         <%=ViewData ["w"] %>

         4。运行效果

  • 相关阅读:
    Dynamics CRM2016 Web API之更新记录
    opencv基础笔记(1)
    HTML5开发移动web应用——SAP UI5篇(6)
    就算你不是电商,你应该为你的电商朋友好好看看这篇文章
    xode5.1.1设置IOS欢迎界面的方法
    Java中的Nested Classes和Inner Classes
    pve之daemon
    对话沈向洋 | 在人工智能最好的时代,寻求可能性比超前顾虑更关键
    .NET Core 2.0 开源Office组件 NPOI
    【转发活动】Hey, 是你吗? | 寻"粉"启示
  • 原文地址:https://www.cnblogs.com/mane/p/1830088.html
Copyright © 2011-2022 走看看