2

I've got a problem using ASP.NET MVC3, AJAX and JQUERY. I've got the following function

[HttpPost]
public bool Update(int id, FormCollection collection)

This is my jQuery Source:

$(document).ready(function () {
    $('#btnUpdate').click(function (e) {
        // Cancel default action
        e.preventDefault();
        var formCollection = $('#formId').serialize();
        $.ajax({
            cache: false,
            type: 'POST',
            url: '@Url.Action("Action","Controller")',
            data: { id: $('#id').val(), collection: formCollection },
            success: function (data) {
                alert(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('Error during process: \n' + xhr.responseText);
            }
        });
    });
});

The id parameter submitted successfully, but the collection (FormCollection) includes an array with {[0]: 10000, [1]: collection}. I can't fix the problem. When I redesign the solution like this:

[HttpPost]
public bool Update(FormCollection collection)

$(document).ready(function () {
    $('#btnUpdate').click(function (e) {
        // Cancel default action
        e.preventDefault();
        $.ajax({
            cache: false,
            type: 'POST',
            url: '@Url.Action("Action", "Controller")',
            data: $('#formId').serialize(),
            success: function (data) {
                alert(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('Error during process: \n' + xhr.responseText);
            }
        });
    });
});

everything works fine. What I'm doing wrong in passing 2 parameter?

THX!!!

2
  • this way not get FormCollection you have pass collection of model class define here. like public bool Update(int id, userProfile collection) Commented Sep 25, 2012 at 6:25
  • make sure in IE check this Is JSON.stringify() supported by IE 8? Commented Sep 25, 2012 at 6:35

4 Answers 4

1

Try calling JSON.stringify() on your JSON:

data: JSON.stringify({ id: $('#id').val(), collection: formCollection })
Sign up to request clarification or add additional context in comments.

Comments

1
$(document).ready(function () {
    $('#btnUpdate').click(function (e) {
        // Cancel default action
        e.preventDefault();
        var formCollection = $('#formId').serialize();
        $.ajax({
            cache: false,
            type: 'POST',
            url: '@Url.Action("Action","Controller")',
            data: JSON.stringify({ id: $('#id').val(), collection: formCollection }),
            success: function (data) {
                alert(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('Error during process: \n' + xhr.responseText);
            }
        });
    });
});

you should have to pass the data with jsonstringify

8 Comments

Creates the following error: The parameters dictionary contains a null entry 'id' of non-nullable type 'System.Int32'...
it means you also have to look on your id field
or make [HttpPost] public bool Update(int id, FormCollection collection) to [HttpPost] public bool Update(int? id, FormCollection collection)
id should not be an optional parameter! jquery and ajax have to submit the unique id and the form collection...
have you checked what you are getting in id through alert
|
0

This is the actual watch: Debugging

Parameter ID submitted successfully (Name=id, Wert=10000). Above id you can see formCollection including FORM.serialize() values of the ajax call. But the System.Web.Mvc.FormCollection including two keys only: id and collection! I expected

  1. "Name"
  2. "Address"
  3. "Address_2"
  4. ...

I think if got a mistake in creating my ajax request, but I don't know why...

var customerNo = $('#fieldID').val();
var formCollection = $('#formID').serialize();
$.ajax({
    cache: false,
    type: 'POST',
    url: '@Url.Action("Action", "Controller")',
    data: { id: customerNo, collection: formCollection },
    dataType: 'json',
    success: function (data) {
        if (data) {
            alert(data);
        };
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert('Error during process: \n' + xhr.responseText);
    }
});

The following line generates an error:

data: JSON.stringify({ id: customerNo, collection: formCollection })

The parameters dictionary contains a null entry 'id' of non-nullable type 'System.Int32'...

Changed without effect:

data: { id: customerNo, collection: JSON.stringify(formCollection) },

Any ideas?

Comments

0

This is my idea (works!)

Add in a partial class from a model an string attribute, add this attribute manually in "data".

JS:

$.ajax({
        cache: false,
        type: 'POST',
        url: '@Url.Action("Action", "Controller")',
        data: $("form").serialize() + "&id=whatever",
        dataType: 'json',
        success: function (data) {
                if (data) {
                        alert(data);
                };
        },
        error: function (xhr, ajaxOptions, thrownError) {
                alert('Error during process: \n' + xhr.responseText);
        }
});

partialClass.cs Model:

public partial class TableName
{
    public string id { get; set; }
}

Controller:

[HttpPost]
public bool Update(FormCollection collection){
    var ID = collection.id; 
}

Regards!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.