asp.net-mvc-4 – 如何在mvc4中将Json字符串发送到Controller并反序列化json

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-4 – 如何在mvc4中将Json字符串发送到Controller并反序列化json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有像下面的json对象
Extension = {
"BookMarks":
[{"Name":"User1","Number":"101"},{"Name":"User2","Number":"102"},{"Name":"User3","Number":"103"}]}

我想将此json字符串发送到我的控制器Action方法并反序列化数据

我想将数据传递给partialview

public ActionResult ExtensionsDialog(var data)
        {
            return PartialView(data); 
        }

任何帮助
提前致谢..

解决方法

在您的视图中:
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..
         }

希望这可以帮助.

猜你在找的asp.Net相关文章