I am having a slight issue sending formCollection data to my controller action method in MVC using jQuery .Post method. While I am sending the data via jQuery, it is not in the formCollection parameter of my action controller, well let me be more specific, it is in there, but not quite how I expected it to be in there... First off, the formCollection parameter now has 2 entries... the PopID passed in (21) plus the serialized data passed in (FirstName=Fifo&LastName=Caputo&DateOfBirth=&DateOfBirth=7%2F29%2F2011+12%3A00%3A00+AM&City=&State=&Country=&Postal+Code=&deathIndicator=&email=&gender=&language=&NextOfKin=&Phone=) What am I doing wrong here? Controller Action.
[HttpPost]
public ActionResult SearchByDemographic(int PopID, FormCollection formCollection)
{
}
JavaScript-Jquery method to pass in values...
$(function () {
$("#DemoGraphSubmit").click(function (e) {
e.preventDefault();
var form = $("#DemoGraphID");
var srlzdform = form.serialize();
var PopID = <% =PopID %>
var options = [];
var serializedForm = form.serialize();
$.post("/PatientACO/SearchByDemographic", {PopID:PopID,srlzdform:srlzdform}, function (data) {
options = $.map(data, function (item, i) {
return "<option value=" + item.Value + ">" + item.Text + "</option>";
});
$("#PatientListToAdd").html(options.join(""));
});
});
});
Any ideas? I am going to keep looking.
Try this:
Although I am not sure why you want to use a FormCollection instead of a viewmodel.
Perhaps you want to take a look at NerdDinner
So here is your view (obviously more complex than this)
Here is your action:
And here is your model
This way if you store your PopID in the model you don't have to do <%: PopID %> in your javascript.