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。运行效果

  • 相关阅读:
    【前端基础】webpack 概述
    Temporary Post Used For Theme Detection (1f3683c2-c37f-4fb8-ba3f-6a33842053d2
    分布式场景实用技术
    Linq中的SingleOrDefault和FirstOrDefault
    笔记:SpringBoot教程快速入门-基础-SpringBoot简介&快速入门案例(一)
    使用nacos的配置中心报错Ignore the empty nacos configuration and get it based on dataId
    Shell 命令
    django orm 笔记
    memoize
    once函数
  • 原文地址:https://www.cnblogs.com/mane/p/1830088.html
Copyright © 2011-2022 走看看