日期:2014-05-17 浏览次数:20448 次
public class JsonModelBinder:IModelBinder
{
public object BindModel(ControllerContext controllerContext,ModelBindingContext modelBindingContext)
{
if(controllerContext==null)
{
throw new ArgumentException("controllerContext");
}
if(modelBindingContext==null)
{
throw new ArgumentException("modelBindingContext");
}
var serialize = new DataContractJsonSerializer(modelBindingContext.ModelType);
return serialize.ReadObject(controllerContext.HttpContext.Request.InputStream);
}
}
[ModelBinder(typeof(JsonModelBinder))]
public class JsonAccountRequest
{
public int Id { get; set; }
public string Name { get; set; }
}
<script type="text/javascript" src="../../Scripts/jquery-1.4.4.js"></script>
<h2>Index</h2>
@using(Html.BeginForm())
{
<input type="text" name="Id" />
<input type="text" name="Name" />
<input type="submit" value="Submit" />
}
<script type="text/javascript" src="../../Scripts/json2.js"></script>
<script type="text/javascript">
$(function () {
var jsonData = JSON.stringify({
"Id": 1,
"Name": "fxp"