如何在ASP.NET MVC中将FormCollection转换为模型 [英] How to convert formcollection to model ins ASP.NET MVC

查看:43
本文介绍了如何在ASP.NET MVC中将FormCollection转换为模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为ClientPackage的模型类,并且在我的控制器操作方法中,我从ajax发布调用中收到了一个FormCollection,我希望将其转换为模型类ClientPackage.

I have a model class called ClientPackage and in my controller action method i receive a FormCollection from an ajax post call that i would like to convert it to the model class ClientPackage.

public class ClientPackage
    {
        public int DemandeId { get; set; }
        public string NumeroRequette { get; set; }
        public string NumeroModification { get; set; }
        public int CasId { get; set; }
        public string NumeroDossier { get; set; }
        public string NomPatient { get; set; }
        public string PrenomPatient { get; set; }
        public string NiveauPriorite { get; set; }
        public int NiveauPrioriteId { get; set; }
        public Unite UniteDepart { get; set; }
        public Unite UniteDestination { get; set; }
        public Demandeur DemandeurDepart { get; set; }
        public Demandeur DemandeurDestination { get; set; }
        public ConditionTransport ConditionTransport { get; set; }
        public Transport Transport { get; set; }

    }

[HttpPost]
        public string AjaxCall(FormCollection formData)
        {
            ClientPackage package = (ClientPackage)formData; //exception error


        }

我会寻求帮助

推荐答案

这是我使用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

        }

希望这会有所帮助!

这篇关于如何在ASP.NET MVC中将FormCollection转换为模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆