我再次需要 Asp.Net MVC(Razor View Engine)的帮助。在我的视图(索引)中,我有
@model IEnumerable<Movie>
我想将模型传递给控制器:我以这种方式对模型进行字符串化:
<script type="text/javascript">
function postData(){
var urlact='@Url.Action("createDoc")';
var model ='@Html.Raw(Json.Encode(Model));
$.ajax({
data: JSON.stringify(model),
type:"POST",
url:urlact,
datatype:"json",
contentType:"application/json; charset=utf-8"
});
}
</script>
一切似乎都有效,因为我知道字符串化数据是:
'[{"ID":1,"Title":"The Lord of the Rings: The Fellowship of the Ring","ReleaseDate":"\/Date(1007938800000)\/","Genre":"Fantasy","Price":93000000.00},{"ID":2,"Title":"The Lord of the Rings: The Two Towers","ReleaseDate":"\/Date(1039042800000)\/","Genre":"Fantasy","Price":94000000.00},{"ID":3,"Title":"The Lord of the Rings: The Return of the King","ReleaseDate":"\/Date(1070233200000)\/","Genre":"Fantasy","Price":94000000.00}]';
问题是:在控制器中,methodName “createDoc”(如脚本中声明的)我无法访问字符串化数据。根据网上建立的一些样本,我的方法是这样的:
[HttpPost]
public ActionResult createDoc(IEnumerable<Movie> movies)
{
//...using movies list
return View();
}
为什么我无法访问字符串化数据?我该怎么做,是否有一些方法可以调用来反序列化 Controller 方法中的模型?另外,我可以使用 serialize() 方法而不是 stringify() 方法吗?如果是这样,视图和控制器方面的语法是什么?
谢谢你。