Nate Pet
Nate Pet

Reputation: 46322

MVC : Pass Model from View through JQuery

I have the following model in my view that I am passing to it:

    @model DB.Models.Employees

after the user updates the model, I like to pass the model through Jquery. I have the following code - note that url is the path to my controller:

     $.post(url, { employeeinfo: '@Model' },
     .....

@Model did not pass the whole model. I get a null value when I view the content of it in the controller.

I am using MVC .NET

Upvotes: 2

Views: 2141

Answers (1)

Ryand.Johnson
Ryand.Johnson

Reputation: 1906

You have to get the model values and put them in a JSON object. Then it will send your model to the server.

    var emp = {
        SearchType: $('#txtFirstName').val(),
        SelectedAccounts: $('#txtLastName').val()
    };

    $.ajax({
        url: "UpdateEmp",
        data: parms,
        type: "POST",
        dataType: 'json',
        success: function (data) {

        },
        error: function () {
            document.location = 'ErrorThrown';
        }

Here is a good article on binding your to your model after it is sent. model binding

Upvotes: 1

Related Questions