MVC3: 页面向服务传参(view->controller,get,post)
- HttpGet
- HttpPost
- GetPost 总结比较
- ajax get post
编辑页面 Views\Movies\Index.cshtml view:
@Html.ActionLink("Edit", "Edit", new { id=item.ID })
链接前台URL显示为:the previous image is http://localhost:xxxxx/Movies/Edit/4. 等同于在IE地址栏输入URL http://localhost:xxxxx/Movies/Edit?ID=4
对应MVC路由 {controller}/{action}/{id}。在controller中代码为:
//
// GET: /Movies/Edit/5
public ActionResult Edit(int id = 0)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
例2 前台:
//Form Get
<form action="/MyTest/TestHttpGet" method="get">
testHttpGet <input type="text" name="text"/>
<br />
<input type="submit" value="Get"/>
</form>
//Html.BeginForm
<%Html.BeginForm("TestHttpGet", "MyTest", FormMethod.Get);%>
username:<%=Html.TextBox("UserName") %>
password:<%=Html.TextBox("PassWord") %>
<input type="submit" value="BeginFormGet"/>
<%Html.EndForm();%>
后台:
public string TestHttpGet()
{
string str = Request.QueryString["text"];
<form action="/MyTest/TestHttpPost" method="post">
testHttpPost <input type="text" name="name"/>
<br />
<input type="submit" value="Post"/>
</form>
<%Html.BeginForm("TestHttpGet", "MyTest", FormMethod.Get);%>
username:<%=Html.TextBox("UserName") %>
password:<%=Html.TextBox("PassWord") %>
<input type="submit" value="BeginFormGet"/>
<%Html.EndForm();%>
</pre>后台:</div><div><pre name="code" class="html"> [HttpPost]
public string TestHttpPost()
{
string str =Request.Form["name"];
string name = Request.Form["Name"];
string pwd = Request.Form["PassWord"];
return "HelloHttpPost "+str+name+pwd;
}
3)Get Post 总结比较
请求一般不应产生副作用。就是说,它仅仅是获取资源信息,就像数据库查询一样,不会修改,增加数据,不会影响资源的状态。幂等:对同一URL的多个请求应该返回同样的结果。
POST是没有大小限制的,HTTP协议规范也没有进行大小限制,说“POST数据量存在80K/100K的大小限制”是不准确的,POST数据是没有限制的,起限制作用的是服务器的处理程序的处理能力。对于ASP程序,Request对象处理每个表单域时存在100K的数据长度限制。但如果使用Request.BinaryRead则没有这个限制。
request forgery攻击。
get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。
看百度百科总结如下,更简短点;
1. get是从服务器上获取数据,post是向服务器传送数据。
2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
3. 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。
4. get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
5. get安全性非常低,post安全性较高。但是执行效率却比Post方法好。 建议:
1、get方式的安全性较Post方式要差些,包含机密信息的话,建议用Post数据提交方式;
2、在做数据查询时,建议用Get方式;而在做数据添加、修改或删除时,建议用Post方式;
4) ajax post get
//View
<script type="text/javascript">
function GetTime() {
$.get("Home/GetTime", function (response) {
$("#myPnl").html(response);
});
return false;
}
</script>
<div id="myPnl" style="width: 300px; height: 30px; border: 1px dotted silver;">
</div>
<a href="#" onclick="return GetTime();">Click Me</a>
//Control
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">public ActionResult GetTime()</span>
{
return Content(DateTime.Now.ToString());
}
@Post
//view
<script type="text/javascript">
$(document).ready(function () {
<span style="white-space:pre"> </span> $.post("Home/GetTime",
{val1:"bailang",city:"beijing"},
<span style="white-space:pre"> </span>function (data,status) {
<span style="white-space:pre"> </span>$("#myPnl2").html(data+status);
});
$("#btn").click(function () {
$.ajax({
type: 'Post',
url: "/MyTest/MyAjax",
data:{
val1: $("#ajaxInput").val(),
val2: $("#ajaxInput2").val(),
val3: "val3333",
val4:"val444"
},
dataType: "json",
success: function (data) {
alert("ajax success");
},
error: function () {
alert("ajax error")
}
});
});
});
//Control
[HttpPost]
public string MyAjax(string val1)
{
string val2 = Request["val2"].ToString();
string val3 = Request.Form["val3"].ToString();
string val4 = Request.Params["val4"].ToString(); return val1;
}
[HttpPost]
public ActionResult PostTime()
{
var fname=Request.Form["name"];
var city=Request.Form["city"];
Response.Write("Dear "+fname);
Response.Write("Hope you live well in " +city);
return Content(DateTime.Now.ToString());
}
[HttpPost]
public ActionResult SaveUser(UserModel userModel)
{
//Save User Code Here
//...... if (Request.IsAjaxRequest())
return Content("Save Complete!");
else
return View();
}
MVC3: 页面向服务传参(view->controller,get,post)的更多相关文章
- 小程序页面跳转传参-this和that的区别-登录流程-下拉菜单-实现画布自适应各种手机尺寸
小程序页面跳转传参 根目录下的 app.json 文件 页面文件的路径.窗口表现.设置网络超时时间.设置多 tab { "pages": [ "pages/index/i ...
- js实现静态页面跳转传参
最近有个项目: 存静态web服务,一个新闻页面列表出所有新闻摘要信息,然后通过点击新闻详情访问到该新闻的详情页面: 新闻展示的页面通过ajax请求接口获取到新闻的摘要信息,预计想通过id的方式访问到新 ...
- 页面ajax请求传参及java后端数据接收
js ajax请求传参及java后端数据接收 Controller: package com.ysl.PassingParameters.controller; import java.util.Li ...
- 微信小程序——动态渲染页面、路径传参
1.动态渲染页面.改变css.样式必须setData渲染过去 this.setData({ userInfo: app.globalData.userInfo, token: app.glob ...
- vue 页面跳转传参
页面之间的跳转传参,正常前端js里写 window.location.href="xxxxx?id=1" 就可以了: 但是vue不一样 需要操作的是路由history,需要用到 V ...
- 【转】request和response的页面跳转传参
下面是一位园友的文章: jsp或Servlet都会用到页面跳转,可以用 request.getRequestDispatcher("p3.jsp").forward(request ...
- vue-router实现登录和跳转到指定页面,vue-router 传参
定义路由的时候可以配置 meta 字段: const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, childr ...
- 小程序页面跳转传参参数值为url时参数时 会出现丢失
当参数的值为url的时候, ?号 _ 下划线 等等 都会被 截取掉,看不到, 这样在 另一个页面 options中 截取的url就不完全 let url="http://ba ...
- 微信小程序页面跳转传参
1.传递参数方法 使用navigatior组件 <navigator url="/pages/pull/pull?title=lalla&name=cc" hov ...
随机推荐
- JS解析URL参数为对象
曲不离口,拳不离手 JS小编程练习之一:解析URL参数为对象 url:http://www.baidu.com/we/index.html?id=098&aaa=123&ccc=456 ...
- Hive常用数据库操作
1.创建表的三种姿势 第一种 //员工表 create table if not exists default.emp( empno int, ename string, job string, mg ...
- Python 操作sqlite数据库及保存查询numpy类型数据(二)
# -*- coding: utf-8 -*- ''' Created on 2019年3月6日 @author: Administrator ''' import sqlite3 import nu ...
- linux NFS 自动挂载
NFS 自动挂载的两种方法 第一种: 需要注意的事项 开机挂载的命令不能写入到/etc/fstab 中,由于 NFS 依赖于网络,而/etc/fstab 的引用是在计算机 网络尚未启动的时候就开始引导 ...
- 统计学习方法——第四章朴素贝叶斯及c++实现
1.名词解释 贝叶斯定理,自己看书,没啥说的,翻译成人话就是,条件A下的bi出现的概率等于A和bi一起出现的概率除以A出现的概率. 记忆方式就是变后验概率为先验概率,或者说,将条件与结果转换. 先验概 ...
- Codeforces Round #426 (Div. 2) - D
题目链接:http://codeforces.com/contest/834/problem/D 题意:给定一个长度为n的序列和一个k,现在让你把这个序列分成刚好k段,并且k段的贡献之和最大.对于每一 ...
- 012-linux系统管理——进程管理与工作管理
linux系统管理——进程管理 top 命令是使用 top - :: up :, user, load average: 0.06, 0.60, 0.48 #五分钟钱,十分钟前,十五分钟前负载的值根据 ...
- Codeforces Round #569 (Div. 2) 题解A - Alex and a Rhombus+B - Nick and Array+C - Valeriy and Dequ+D - Tolik and His Uncle
A. Alex and a Rhombus time limit per test1 second memory limit per test256 megabytes inputstandard i ...
- 神经风格转换Neural Style Transfer a review
原文:http://mp.weixin.qq.com/s/t_jknoYuyAM9fu6CI8OdNw 作者:Yongcheng Jing 等 机器之心编译 风格迁移是近来人工智能领域内的一个热门研究 ...
- airflow 简介
转载:https://zhuanlan.zhihu.com/p/36043468 简介 Apache-Airflow 是Airbnb开源的一款数据流程工具,目前是Apache孵化项目.以非常灵活的方式 ...