1
$.ajax({
    type: 'POST',
    url: '@Url.Action("AccountUpdate", "Customer")',
    async: false,
    data: { a:"ed", formCollection: $("#form1").serialize() }
});

Controller:-

public void AccountUpdate(string a, FormCollection formCollection) {}

Question:- In controller AccountUpdate I am getting parameter a ="ed" which is fine. But in FormCollection object I am getting the formCollection object and also the 'a' object Why the form collection object receiving the 'a' object ? It should be only formCollection object

7
  • Because your parameter is typeof FormCollection which is the collection of all submitted data. Replace it with YourModel formCollection Where YourModel is the model you used in the view. Commented Sep 24, 2015 at 9:01
  • So I changed my Controller's parameter to public void AccountUpdate(string a, Customer formCollection) {} No change in Ajax, Now I am getting null in formCollection ? Commented Sep 24, 2015 at 9:19
  • Also what I read is formCollection contains only those elements which are in the scope of form , "a" is not defined in form scope Commented Sep 24, 2015 at 9:23
  • Because your ajax data option is not correct. Just start by using data: $("#form1").serialize(), to ensure your model is binding correctly and I'll then show you how to send back the additional parameter Commented Sep 24, 2015 at 9:23
  • I did that before when I use only data: $("#form1").serialize() it works perfectly Commented Sep 24, 2015 at 9:26

2 Answers 2

1

The parameter of your POST method is typeof FormCollection which is a class that holds all submitted key/value pairs.

Change your method to

[HttpPost]
public void AccountUpdate(string a, Customer model)

And change your script to allow both your model and the additional value to be submitted and bound in the method

var data = $("#form1").serialize() + '&' + $.param({ 'a': 'ed'});

$.ajax({
    type: 'POST',
    url: '@Url.Action("AccountUpdate", "Customer")',
    data: data
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou LeftyX and Stephen Muecke. Stephen Muecke your code is working perfectly now,
1
data: $("#form1").serialize() 

is the way to go.

If you want to add some extra parameters:

$.ajax({
    type: 'POST',
    url: '@Url.Action("AccountUpdate", "Customer")',
    // async: false,
    data: "a=ed&" + $("form1").serialize()
});

This way you can bind directly to your view model:

[HttpPost]
public ActionResult Index(string a, Customer customer)
{
    ...
}

1 Comment

Thanks Lefty, but customer object is null now This is my element in form <input type="text" name="ContactNumber" />

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.