如何发送JSON字符串到控制器在mvc4和反序列化JSON [英] How to Send Json String to Controller in mvc4 and Deserialize json
本文介绍了如何发送JSON字符串到控制器在mvc4和反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有JSON对象像下面
I have the json object like below
Extension = {
"BookMarks":
[{"Name":"User1","Number":"101"},
{"Name":"User2","Number":"102"},
{"Name":"User3","Number":"103"}]}
我想这个JSON字符串发送到我的控制器的操作方法和反序列化数据
I want to send this json string to my controller Action method and Deserialize the data
我想将数据传递给partialview
I want to pass the data to the partialview
public ActionResult ExtensionsDialog(var data)
{
return PartialView(data);
}
任何帮助
在此先感谢..
Any help Thanks in advance..
推荐答案
在您的查看
function SendData(){
var dataToSend = JSON.stringify(data);
$.ajax({
type: "POST",
url: '@Url.Action("YourAction", "YourController")',
dataType: "json",
data: dataToSend,
contentType: "application/json; charset=utf-8",
});
}
$("#Updatebtn").click(function () {
sendData();
});
在您模型
public class YourModel
{
public String Name { get; set; }
public int Number { get; set; }
}
在您的控制器
[HttpPost]
public ActionResult YourAction()
{
var resolveRequest = HttpContext.Request;
List<YourModel> model = new List<YourModel>();
resolveRequest.InputStream.Seek(0, SeekOrigin.Begin);
string jsonString = new StreamReader(resolveRequest.InputStream).ReadToEnd();
if (jsonString != null)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
model = (List<YourModel>)serializer.Deserialize(jsonString, typeof(List<YourModel>);
}
//Your operations..
}
希望这有助于。
这篇关于如何发送JSON字符串到控制器在mvc4和反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文