jquery ajax/post/get 传参数给 mvc的action
jquery ajax/post/get 传参数给 mvc的action
1.ActionResult Test1
2.View Test1.aspx
3.ajax page
4.MetaObjectMigration.cs string json convert to class
5.相关的代码下载(包含用的相关类, jquery.json.js等)
ActionResult Test1
public ActionResult Test1(string nameJS, UserInfoInputData model, string js)
{
UserInfoInputData userinfo = new UserInfoInputData();
if (!string.IsNullOrEmpty(js))
{
userinfo = (UserInfoInputData)js.ToInputDataObject(typeof(UserInfoInputData));
} ViewData["Time"] = model.Name + " :" + userinfo.Name;
ViewData["Time2"] = model.age;
ViewData["Message"] = "Test1 :" + nameJS + " :" + typeof(UserInfoInputData).ToString(); ViewData["js"] = userinfo.ToJSON(); return View();
}
Test1.aspx
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Test1</title>
</head>
<body>
<div>
The current time is: <%= DateTime.Now.ToString("T") %>
<br/><br/>
BO:<%=ViewData["Time"] %>
<br/><br/>
BO2:<%=ViewData["Time2"] %>
<br/><br/>
Message:<%=ViewData["Message"] %>
<br/><br/>
<%=ViewData["js"]%>
</div>
</body>
</html>
ajax page 四种写法
function test(parameters) {
var sjson = '{ "name~!@#$%^&*(){}|:\"<>?/.,\';\\[]v-name": "nvar", "desc": "des" } ';
var sjs = '{"Name":"jsname", "age":3}';
//get post 都可以
$.post("Test1", "nameJS=" + encodeURIComponent(sjson) + "&model.name=modelName&model.age=3" + "&js=" + encodeURIComponent(sjs));
//model.name model.Name 都可以
var json = { "nameJS": "~!@#$%^&*(){}|:\"<>?/.,';\\[]v-name",
"model.name": "modelname", "model.age": 1,
"js":'{"Name":"jsname", "age":3}'
};
$.post("Test1", json);
var param = {};
param["nameJS"] = "paramjs";
param["model.Name"] = "someone";
param["model.age"] = 2;
param["js"] = '{"Name":"jsname", "age":3, "Tags":"tag1"}';
//或者param["js"] = JSON.stringify({"Name":"jsname", "age":3, "Tags":"tag1"});
$.post("Test1", param);
var metaformJsonItem = new Object();
metaformJsonItem.nameJS = "~!@#$%^&*(){}|:\"<>?/.,';\\[]v-name";
metaformJsonItem.js = JSON.stringify({
//key:value key注意大小写
"Name": "~!@#$%^&*(){}|:\"<>?/.,';\\[]v-jsname",
"Tags": JSON.stringify(["tag1", "tag2"]),
"age": 3,
"Ids": JSON.stringify([1, 2, 3]), //或者'[1, 2, 3]'
"Country": 0,
"Countries": JSON.stringify([1, 2])
});
metaformJsonItem["model.Name"] = "modelname";
metaformJsonItem["model.age"] = "11";
$.post("Test1", metaformJsonItem);
}
string json convert to object class
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using Newtonsoft.Json; namespace Demo.Common.Metaform.UI
{
public static class MetaObjectMigration
{
private enum HandlingMethod
{
DoNothing,
SimpleEnum,
ArrayOfEnum,
ArrayOfString,
ListOfEnum,
ListOfSerializable
} public static InputDataObject ToInputDataObject(this string jsonXml, Type objectType)
{
return jsonXml.FromMetaJson(objectType); ;
} public static InputDataObject FromMetaJson(this string json, Type objectType)
{
string jsonString = GetJsonFromMetaJson(json, objectType); JsonSerializer serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.MissingMemberHandling = MissingMemberHandling.Ignore; InputDataObject deserialedObject =
(InputDataObject) serializer.Deserialize(new StringReader(jsonString), objectType); return deserialedObject; } private static string GetJsonFromMetaJson(string json, Type displayObjectType)
{
PropertyInfo[] properties = displayObjectType.GetProperties(); using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
{
using (StringWriter sw = new StringWriter())
{
using (JsonTextWriter writer = new JsonTextWriter(sw))
{
HandlingMethod handlingMethod = HandlingMethod.DoNothing;
bool ignoreThisProperty = false;
string newKey = string.Empty;
int arrayLevel = ;
Type elementType; while (reader.Read())
{
if (reader.TokenType == JsonToken.PropertyName)
{
string propertyJsonName = reader.Value.ToString();
var propertyName = propertyJsonName;//JsonNameToPropertyName(propertyJsonName); PropertyInfo propertyInfo = properties.FirstOrDefault(c => (c.Name == propertyName)); if (propertyInfo != null)
{
ignoreThisProperty = false; var propertyType = propertyInfo.PropertyType;
if (propertyType.IsEnum)
{
handlingMethod = HandlingMethod.SimpleEnum;
}
else if (propertyType.IsGenericType && propertyType.GetGenericArguments()[].IsEnum)
{
elementType = propertyType.GetGenericArguments()[];
handlingMethod = HandlingMethod.ListOfEnum;
}
else if (propertyType.IsGenericType &&
propertyType.GetGenericArguments()[].IsSerializable)
{
elementType = propertyType.GetGenericArguments()[];
handlingMethod = HandlingMethod.ListOfSerializable;
}
else if (propertyType.IsArray && propertyType.GetElementType().IsEnum)
{
elementType = propertyType.GetElementType();
handlingMethod = HandlingMethod.ArrayOfEnum;
}
else if (propertyType.IsArray)
{//e.g. string[]
elementType = propertyType.GetElementType();
handlingMethod = HandlingMethod.ArrayOfString;
}
else
{
handlingMethod = HandlingMethod.DoNothing;
}
}
else
{
ignoreThisProperty = true;
continue;
}
newKey = propertyJsonName;//JsonNameToPropertyName(propertyJsonName);
writer.WritePropertyName(newKey);
}
else if (reader.TokenType == JsonToken.String || reader.TokenType == JsonToken.Integer)
{
if (ignoreThisProperty)
continue; string value = reader.Value.ToString();
if (handlingMethod == HandlingMethod.SimpleEnum)
{
int code;
if (int.TryParse(value, out code))
{
writer.WriteValue(code);
}
else
{
var intList = value.ToIntList();
if (intList != null && intList.Count > )
{
writer.WriteValue(intList[]);
}
else
{
writer.WriteNull();
}
}
}
else if (handlingMethod == HandlingMethod.ListOfEnum ||
handlingMethod == HandlingMethod.ArrayOfEnum ||
handlingMethod==HandlingMethod.ArrayOfString ||
handlingMethod == HandlingMethod.ListOfSerializable)
{
CreateJsonArray(writer, handlingMethod, value, arrayLevel);
}
else
{
writer.WriteValue(value);
}
}
else
{
//Json Clone
switch (reader.TokenType)
{
case JsonToken.Comment:
writer.WriteComment(reader.Value.ToString());
break;
case JsonToken.EndArray:
writer.WriteEndArray();
arrayLevel--;
break;
case JsonToken.EndConstructor:
writer.WriteEndConstructor();
break;
case JsonToken.EndObject:
writer.WriteEndObject();
break;
case JsonToken.None:
break;
case JsonToken.Null:
writer.WriteNull();
break;
case JsonToken.StartArray:
writer.WriteStartArray();
arrayLevel++;
break;
case JsonToken.StartConstructor:
writer.WriteStartConstructor(reader.Value.ToString());
break;
case JsonToken.StartObject:
writer.WriteStartObject();
break;
case JsonToken.Undefined:
writer.WriteUndefined();
break;
default:
writer.WriteValue(reader.Value);
break;
}
}
} return sw.ToString();
}
}
}
} private static void CreateJsonArray(JsonTextWriter writer, HandlingMethod handleingMethod, string value, int arrayLevel)
{
IList valueList;
if (handleingMethod == HandlingMethod.ListOfEnum || handleingMethod == HandlingMethod.ArrayOfEnum)
{
valueList = value.ToIntList();
}
else
{
valueList = value.ToStringList();
} if (valueList.Count > )
{
if (arrayLevel == )
{
writer.WriteStartArray();
} foreach (var i in valueList)
{
writer.WriteValue(i);
} if (arrayLevel == )
{
writer.WriteEndArray();
}
}
else
{
if (arrayLevel == )
{
writer.WriteStartArray();
writer.WriteEndArray();
}
}
} } }
jquery ajax/post/get 传参数给 mvc的action的更多相关文章
- [转载]jquery ajax/post/get 传参数给 mvc的action
jquery ajax/post/get 传参数给 mvc的action 1.ActionResult Test1 2.View Test1.aspx 3.ajax page 4.MetaO ...
- ASP.NET 异步Web API + jQuery Ajax 文件上传代码小析
该示例中实际上应用了 jquery ajax(web client) + async web api 双异步. jquery ajax post $.ajax({ type: "POST&q ...
- ajax向php传参数对数据库操作
刚入门php,要求要对多用户进行批量删除(当然实际中是不可能的),在这就以此为例. 大意就是通过对数据库中用户查询,将用户信息显示在页面表格中,在进行多项选择后将所选行参数通过ajax传入后台php文 ...
- JQuery $.ajax(); 异步访问完整参数
$.ajax 完整参数 jquery中的ajax方法参数 url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post ...
- 兼容ie的jquery ajax文件上传
Ajax文件上传插件很多,但兼容性各不一样,许多是对ie不兼容的,另外项目中是要求将网页内容嵌入到桌面端应用的,这样就不允许带flash的上传插件了,如:jquery uploadify...悲剧 对 ...
- struts2+jquery+ajax实现上传&&校验实例
一直以为ajax不能做上传,直到最近看了一些文章.需要引入AjaxFileUploaderV2.1.zip,下载链接:http://pan.baidu.com/s/1i3L7I2T 代码和相关配置如下 ...
- js数组作为参数用ajax向后台传参数
/*前台往后台传参数时,可以这样写*/ var chessId = "123"; var i=0; var data = []; /*添加单个参数*/ data.push({nam ...
- jquery.ajax中的ifModified参数的误解
原来以为ifModified是为了在AJAX请求是发送 If-Modified-Since头,让服务端返回304. 测试代码如下: $(function () { test(); window.set ...
- JQuery Ajax 向后台传参方式
在jquery的ajax函数中,可以传入3种类型的数据 文本:"uname=alice&mobileIpt=110&birthday=1983-05-12" jso ...
随机推荐
- Microsoft Visual C++ Runtime error解决方法
1: 当出现下图时提示Microsoft Visual C++ Runtime error 2:此时不要关闭该对话框,然后打开任务管理器(Ctrl+Shift+Esc)如下图: 找到Microsoft ...
- 在Silverlight宿主html页面添加按钮无法显示
在建silverlight应用程序时宿主html中嵌入的silverlight时出现的问题: 预想效果: 实际效果: silverlight填满整个page的所以无法显示html中其他的控件 解决办法 ...
- Python脚本控制的WebDriver 常用操作 <六> 打印当前页面的title及url
下面将使用WebDriver来答应浏览器页面的title和访问的地址信息 测试用例场景 测试中,访问1个页面然后判断其title是否符合预期是很常见的1个用例: 假设1个页面的title应该是'hel ...
- 转载: android 学习架构
http://www.cnblogs.com/forlina/archive/2011/06/29/2093332.html 引言 通过前面两篇: Android 开发之旅:环境搭建及HelloWor ...
- MIFARE系列5《存储结构》
Mifare S50把1K字节的容量分为16个扇区(Sector0-Sector15),每个扇区包括4个数据块(Block0-Block3),我们也将16个扇区的64个块按绝对地址编号为0~63,每个 ...
- .NET开源工作流RoadFlow-流程设计-流程步骤设置-按钮设置
按钮设置是配置当前步骤的处理者可以执行哪些操作,每个按钮都有对应的执行脚本(javascript脚本). 从左边的按钮列表中选择当前步骤需要的按钮. 注意:如果是流程最后一步则要配置完成按钮而不是发送 ...
- Mybatis typeAliases别名
<typeAliases> <typeAlias type="com.green.phonemanage.model.CellPhone" alias=" ...
- Should .close() be put in finally block or not?
The following are 3 different ways to close a output writer. The first one puts close() method in tr ...
- Spring Roo
Spring Roo 是SpringSource新的开放源码技术,该技术主要面向企业中的Java开发者,使之更富有成效和愉快的进行开发工作,而不会牺牲工程完整或灵活性.无论你是一个新的Java开发人员 ...
- [转]Win7 UAC的安全、兼容及权限
[转]Win7 UAC的安全.兼容及权限 http://www.cnblogs.com/mydomain/archive/2010/11/24/1887132.html 网上关于这个问题讨论较多,但也 ...