80126

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

Question:

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.

Answer1:

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 <a href="http://www.asp.net/mvc/tutorials/nerddinner" rel="nofollow">NerdDinner</a>

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.

Recommend

  • How to externalize Grails data config
  • vb.net 2008 textbox max length
  • MySQL Query performance help, many of the same table being joined
  • Specifying a Date Range in Google Custom Search api
  • Failed to convert property value of type 'java.lang.String' to required type 'java.ut
  • Postgresql: removing spaces between certain type of digits
  • Conditional Validation in Ecto for OR - 1 of 2 fields is required
  • Asp.net ajax with mvc2
  • mysql partitioning
  • send value from ember component to parent template
  • Pulling Columns from Excel
  • Simultaneously melt multiple columns in Python Pandas
  • Efficiently remove lines from fileA that contains string from fileB
  • json showing duplicate output of mysql result
  • Using @JsonIgnore on my getter is making it so that my variable doesn't show up in the POST mod
  • Parsing/Looping over JSON objects from a text file (Python)
  • XPath expression with white space in node for defiant.js
  • Login to a website using python
  • $.ajax & passing data to .asmx webservice
  • Wordpress plugin for dependent drop down lists
  • sql for calculating points for games
  • JPA - getting distinct value from one column
  • php regex remove digits
  • Add delivery info to query in SAP Crystal Reports
  • Align Excel cell to center VB - xlCenter is not declared
  • Update a record where _id = :id with Mongoose
  • @Autowired for @ModelAttribute
  • Django: ORDER BY DESC on FloatField puts null values top?
  • Angular2 ag-grid datasource not working
  • How to make R's read_csv2() recognise the text characters properly
  • Implementation of State Monad
  • How do I access an unhandled exception in an MVC Error view?
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Email verification using google app script and google forms
  • Email format validation in mvc3 view
  • sending/ receiving email in Java
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal