且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.NET MVC中将FormCollection转换为模型

更新时间:2022-11-15 17:14:35

这是我使用ajax发布模型的方式.

Here is how I post models with ajax.

<script>
function PostForm() {
        var model = $('#your_form_id').serialize();
        $.ajax({
            url: '/YourController/AjaxCall',
            type: 'POST',
            data: model,
            success: function (data) {


            },
            error: function (request, error) {
                console.log("Request: " + JSON.stringify(request));
            }
        });
    }
</script>

和您的控制器中.

[HttpPost]
        public string AjaxCall(ClientPackage model)
        {

             //no need to cast the model to a ClientPackage
             //ASP.NET mvc will do it for you as long as you send a serialized form that represents a ClientPackage object

        }

希望这会有所帮助!