您的位置:首页 > 其它

MVC入门学习笔记(八)

2010-09-18 11:50 429 查看
七.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。运行效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: