MVC中的QueryString传值

来源:互联网 发布:linux自动重启脚本 编辑:程序博客网 时间:2024/04/29 05:57
 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"] %>

原创粉丝点击