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 = 0;
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()[0].IsEnum)
{
elementType = propertyType.GetGenericArguments()[0];
handlingMethod = HandlingMethod.ListOfEnum;
}
else if (propertyType.IsGenericType &&
propertyType.GetGenericArguments()[0].IsSerializable)
{
elementType = propertyType.GetGenericArguments()[0];
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 > 0)
{
writer.WriteValue(intList[0]);
}
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 > 0)
{
if (arrayLevel == 0)
{
writer.WriteStartArray();
} foreach (var i in valueList)
{
writer.WriteValue(i);
} if (arrayLevel == 0)
{
writer.WriteEndArray();
}
}
else
{
if (arrayLevel == 0)
{
writer.WriteStartArray();
writer.WriteEndArray();
}
}
} } }

相关代码下载

原文地址:http://www.cnblogs.com/dfg727/archive/2013/08/10/3250548.html

[转载]jquery ajax/post/get 传参数给 mvc的action的更多相关文章

  1. jquery ajax/post/get 传参数给 mvc的action

    jquery ajax/post/get 传参数给 mvc的action1.ActionResult Test1    2.View  Test1.aspx3.ajax page4.MetaObjec ...

  2. ASP.NET 异步Web API + jQuery Ajax 文件上传代码小析

    该示例中实际上应用了 jquery ajax(web client) + async web api 双异步. jquery ajax post $.ajax({ type: "POST&q ...

  3. ajax向php传参数对数据库操作

    刚入门php,要求要对多用户进行批量删除(当然实际中是不可能的),在这就以此为例. 大意就是通过对数据库中用户查询,将用户信息显示在页面表格中,在进行多项选择后将所选行参数通过ajax传入后台php文 ...

  4. JQuery $.ajax(); 异步访问完整参数

    $.ajax 完整参数   jquery中的ajax方法参数 url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post ...

  5. 兼容ie的jquery ajax文件上传

    Ajax文件上传插件很多,但兼容性各不一样,许多是对ie不兼容的,另外项目中是要求将网页内容嵌入到桌面端应用的,这样就不允许带flash的上传插件了,如:jquery uploadify...悲剧 对 ...

  6. struts2+jquery+ajax实现上传&&校验实例

    一直以为ajax不能做上传,直到最近看了一些文章.需要引入AjaxFileUploaderV2.1.zip,下载链接:http://pan.baidu.com/s/1i3L7I2T 代码和相关配置如下 ...

  7. js数组作为参数用ajax向后台传参数

    /*前台往后台传参数时,可以这样写*/ var chessId = "123"; var i=0; var data = []; /*添加单个参数*/ data.push({nam ...

  8. jquery.ajax中的ifModified参数的误解

    原来以为ifModified是为了在AJAX请求是发送 If-Modified-Since头,让服务端返回304. 测试代码如下: $(function () { test(); window.set ...

  9. [转载]Jquery Form插件表单参数

    表单插件API提供了几个方法,让你轻松管理表单数据和进行表单提交. ajaxForm增 加所有需要的事件监听器,为AJAX提交表单做好准备.ajaxForm不能提交表单.在document的ready ...

随机推荐

  1. MySql索引的优缺点

    优点 有了索引.对于记录数量很多的表,可以提高查询速度. 缺点 索引是占用空间的. 索引会影响update insert delete速度 ALERT!!! 1.索引要创建在where和join用到的 ...

  2. IIS日志

    1.认识IIS日志 IIS日志默认存放在System32\LogFiles目录下,使用W3C扩展格式.下面我们通过一条日志记录来认识它的格式 2005-01-0316:44:57218.17.90.6 ...

  3. 去重 oracle

    --去重DELETE FROM DEPR_MONTHS_LIST AWHERE (A.ASSET_ID,A.DEPR_DATE,A.UNIT_COST_ID) IN(SELECT B.ASSET_ID ...

  4. html5 canvas 圆形抽奖的实例

    年底没啥,抽空学习了canvas,写了个html5抽奖的代码,造了个轮子,有用的童鞋可以拿走. 其中,canvas.onclick触发抽奖行为,概率可以在core.lottery()函数上添加,美化也 ...

  5. OpenGL9-(FreeImage)加载图片-作为纹理

    /*** 这个例子展示如何使用FreeImage加载图片作为纹理* 初学者,在学习OpenGL的时候,往往因为OpenGL读图片没有那么方便* 而浪费了大量的时间在研究图片格式上,其实大可不必. 1. ...

  6. (转)Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群

    本文的英文版本链接是 http://www.mrxuri.com/index.php/2013/11/20/install-mysql-cluster-on-ubuntu-12-04-lts.html ...

  7. java web 简单的分页显示

    题外话:该分页显示是用 “表示层-控制层-DAO层-数据库”的设计思想实现的,有什么需要改进的地方大家提出来,共同学习进步. 思路:首先得在 DAO 对象中提供分页查询的方法,在控制层调用该方法查到指 ...

  8. java查询WFS服务

    在我们访问wfs服务时候,有时候会遇到前台访问时候的跨域问题.这里给出java访问的一个小例子. import java.io.BufferedReader; import java.io.IOExc ...

  9. TIMAC 学习笔记(二)

    昨天大体上熟悉了TIMAC自带的CC2530的示范例程,今天先从演示抓包入手,分析四种不同的配置工程在空中传输的差异.随后,会按照扫描.组网.入网等MAC层接口函数入手,结合IEEE 802.15.4 ...

  10. iOS SEL的简单总结

    @interface Person : NSObject + (void)test1; - (void)test2; @end // 根据.h文件中定义的Person类和方法 执行完这行代码 在内存中 ...