Json将null传递给控制器

时间:2016-02-21 11:11:39

标签: json asp.net-mvc-4 knockout.js

控制器始终为空。为什么?如何解决这个问题?

控制器:

[HttpPost]
public JsonResult Test(List<Employee> myList)
{
    return Json(myList);
}

Index.cshtml:

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<script src="~/Scripts/knockout-2.2.0.js"></script>

<button data-bind="click: sendItems">Send</button>


<script type="text/javascript">

function MyViewModel() {
    items = ko.observableArray([{ name: 'Jhon', year: '1950' }, { name: 'Nick', year: '1960' }, { name: 'David', year: '1970' }]);
    sendItems= function() {
        $.ajax({  
            type: "POST",
            url: "@Url.Action("Test")",
            data:JSON.stringify(items),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data) {
                alert(ko.toJSON(data));
            }
        }
    });
}
}

ko.applyBindings(new MyViewModel());
</script>

型号:

public class Employee
{
    public string name { get; set; }

    public string year { get; set; }
}

0 个答案:

没有答案