Pass model to Controller from jquery ajax

馋奶兔 提交于 2020-01-25 03:37:04

问题


How can I pass model to jquery ajax my code id '@Model' is the model on the view.i need to pass this model to controller which accept argument of type login model.

 $('#reject1').click( function() {
          var model=@Model;
                $.ajax({
                    cache:true,
                    type: "POST",
                    url: "@(Url.Action("Login", "Customer"))",
                    data:'model='+model,
                    success: function()
                    {

                     //Some logic
                    },

                    complete : function() {}
                });
                return false;
            });

Above code doesn't work


回答1:


For corrected post data you should serialize model

data: $('#FormId').serialize()

And form as:

@using ( @Html.BeginForm( "Login", "Customer", FormMethod.Post, new { id = "FormId" } ) )
{
    //Fields in view
    @Html.EditorFor(x=>x.Name)
    @Html.EditorFor(x=>x.Pass)
}


来源:https://stackoverflow.com/questions/16981941/pass-model-to-controller-from-jquery-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!