Model Binding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        //public List<KeyPair> Numbers { get; set; }
        public object Numbers { get; set; }
    }

    public class KeyPair
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }

    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        public void Post(Person data)
        {
            var numbers= data.Numbers as List<KeyPair>;
        }

    }
}
@{
    ViewBag.Title = "Index";
}
<button id="btn">TEST</button>
<h2>Index</h2>
<script src="~/Scripts/jquery-2.0.3.js"></script>
<script>
    var data = {
        Name: "tom",
        Age: 10,
        Numbers: [{ Key: "a", Value: "A" }, { Key: "b", Value: "B" }]
    };
    $(function() {
        $("#btn").on("click", function() {
            $.ajax({
                url: "/Home/Post",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data:JSON.stringify(data),
                dataType: "Json"
            });
        });
    });
</script>

 

posted @ 2013-08-26 00:44  chunchill  阅读(266)  评论(0编辑  收藏  举报