Ajax 如何将formcollection转换为ASP.NET MVC模型

Ajax 如何将formcollection转换为ASP.NET MVC模型,ajax,asp.net-mvc,Ajax,Asp.net Mvc,我有一个名为ClientPackage的模型类,在我的控制器操作方法中,我从一个ajax post调用接收到一个FormCollection,我想将其转换为模型类ClientPackage public class ClientPackage { public int DemandeId { get; set; } public string NumeroRequette { get; set; } public string Numero

我有一个名为ClientPackage的模型类,在我的控制器操作方法中,我从一个ajax post调用接收到一个FormCollection,我想将其转换为模型类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; }

    }

我希望得到任何帮助

以下是我如何使用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>

希望这有帮助

您可以使用FromForm属性并将模型作为参数接收,而不是尝试转换

[HttpPost]
public ActionResult CreateClientPackage([FromForm] ClientPackage clientPackage)
{
...
}
[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

        }
[HttpPost]
public ActionResult CreateClientPackage([FromForm] ClientPackage clientPackage)
{
...
}