Nitin Varpe
Nitin Varpe

Reputation: 10694

Pass model to Controller from jquery ajax

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

Upvotes: 1

Views: 7910

Answers (1)

Xordal
Xordal

Reputation: 1369

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)
}

Upvotes: 6

Related Questions