开发者

passing FormCollection to controller via JQuery Post method and getting data back...

开发者 https://www.devze.com 2023-03-24 01:54 出处:网络
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

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, we开发者_StackOverflowll 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:

$.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消