Request.Form与Request.QueryString使用

2023-06-14,,

controler控制器代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace asp.net_mvc_Demo2.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            #region Request.QueryString
            ////第一步,设置与前台交互
            ////Request.QueryString,用于method的“Get”获取的是前台输入的查询字符串,
            ////http://localhost:27123/Home/Index?key=1245
            //string str = Request.QueryString["key"];//返回的是查询字符串中“?”后的数值
            ////第二步:业务逻辑处理
            //str = str ?? string.Empty;//新语法,如果str为空则返回string.Empty否则则为str
            ////第三步:将数据返回前台
            //ViewData["demo"] = str;
       
            #endregion
            #region Request.Form使用
            //Request.Form中使用的是Method的方式是“post”,得必须提交给服务器,例如使用submit表单
            string str1 = Request.Form["txt"];
            str1 = str1 ?? "获取值为空";
            TempData["demo1"] = str1; 
            #endregion
            return View(); 
        }

    }
}
View视图代码
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" >
        <div>
<%--            <%:ViewData["demo"] %>--%>
            <%:TempData["demo1"] %>
            <input type="text" name="txt" value="Hello" />
            <input  type="submit" value="anniu"/>
        </div>
    </form>
</body>
</html>

《Request.Form与Request.QueryString使用.doc》

下载本文的Word格式文档,以方便收藏与打印。