0

I have a form send POST data to an action, so I creating an action with multiple parameters is a FormCollection and a string. But I don't know how to pass it into AJAX jQuery.

This is my action method:

public ActionResult Edit(FormCollection form, string CodeName)
{
        List<Product> PList = (List<Product>)TempData["PListIndex"];
        var index = PList.FindIndex(c => c.Code == CodeName);
        PList[index].NameProduct = form[0];
        PList[index].Price = form[1];
        PList[index].Madein = form[2];

        if (FileFactory.Save(PList, @"DATA\CSDL.DAT"))
            return Json("Success", JsonRequestBehavior.AllowGet);

        return Json("Failed to Save", JsonRequestBehavior.AllowGet);
}

This is my Ajax jQuery:

function sendformdata() {
     var form = $("#Edit").serialize();
        $.ajax({
            type: "POST",
            url: '@Url.Action("Edit", "FileEx")',
            data: { form: form, CodeName: _codename } // I don't know how to pass these two parameters
            success: function (response) {
            alert(response);
        }
    });
}
1
  • Use a proper model, not FormCollection Commented Jan 10, 2020 at 22:35

1 Answer 1

1

Use serializeArray() instead of serialize(), then you can add additional elements to the array.

function sendformdata() {
  var form = $("#Edit").serializeArray();
  form.push({ name: "CodeName", value: _codename});
  $.ajax({
    type: "POST",
    url: '@Url.Action("Edit", "FileEx")',
    data: form,
    success: function(response) {
      alert(response);
    }
  });
}

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.