0

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.

1 Answer 1

2

Try this:

$.post("/PatientACO/SearchByDemographic", srlzdform, function (data) {

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)

@model TestModel

<form action="@Url.Action("SearchByDemographic","PatientACO")" method="post">
    @Html.HiddenFor(model => model.PopID)
    @Html.TextBoxFor(model => model.Text)
</form>

Here is your action:

[HttpPost]
public ActionResult SearchByDemographic(TestModel model)
{
}

And here is your model

public class TestModel
{
    public int PopID { get; set; }
    public string Text { get; set; }
    // more properties
}

This way if you store your PopID in the model you don't have to do <%: PopID %> in your javascript.

Sign up to request clarification or add additional context in comments.

1 Comment

What is that doing? How is that different from what I have? What is a viewModel and why would I use that instead of a formCollection? I am collecting data from a user in the form... And note, my popID is the first parameter in my action control How would I handle the incoming data in my controller btw?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.