views:

206

answers:

2

Using the following form in a view

<% using (Ajax.BeginForm("PatientSearch", new {}, new AjaxOptions()
           {
               HttpMethod = "POST",
               UpdateTargetId = "searchResults",
               OnBegin = "BeginRequest",
               OnSuccess = "SuccessRequest",
               OnFailure = "FailRequest"
           }))
           { %>
    <%=Html.DropDownList("patientType",(SelectList)ViewData["PatientTypeList"]) %>
    <%=Html.DropDownList("edCenterID",(SelectList)ViewData["EdCenterList"]) %><br />
    <input type="submit">
<%} %>

Results in the following HTML

<select id="patientType" name="patientType">
  <option selected="selected">Referral</option>
  <option>Patient</option>
</select>
<select id="edCenterID" name="edCenterID">
  <option value="2">Barren River District Health Department</option>
  <option value="3">Madison County Health Department</option>
</select>

and I attempt to catch the values with the code in the my controller

 public ActionResult PatientSearch(string patientType, int edCenterID)        {   
   //do something with values

}

patientType is always passed as ""; however, edCenterID is posted and received just fine.

If I change this from an Ajax.BeginForm to an HTML.BeginForm, everything works perfectly.

Is the problem in my DropDownList, my controller, both?

ADDED FROM COMMENTS

@Eoin pointed out to me that my select is rendering without a value and that is probably the problem. That brings up two questions:
1. Why does it work with the a standard post, but not an Ajax post.
2. How do I get my dropdownlist to contain a value with the list is a simple string only list (no key). I am posting the code used to generate my SelectList below

ViewData["PatientTypeList"]=new SelectList(new List<string>() 
        { "Referral", "Patient"});
+2  A: 

Does the patientType List have a .Value

By the looks of things, theres no value being rendered for the PatientType DDL <option>'s so essentially every option has a value="" so it is actually post back the correct value.

The problem is with what's being rendered in the first place.

edit

Try

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("referral", "referral");
d.Add("patient", "patient");

ViewData["PatientTypeList"]=new SelectList(d, "Key", "Value");
Eoin Campbell
patientType value should be the same as the displayed valueI kind of thought it was the missing value tag, but why would it work with the standard submit and not the ajax submit?
I create the SelectList with the following code: ______ ViewData["PatientTypeList"]=new SelectList(new List<string>() { "Referral", "Patient"}); ____ Is there a better way for a list without an id? (I'll post this as another question if I need. Thanks for your help so far!
How do you define your `EdCenterList` for comparison
Eoin Campbell
might also be better to add this information to the original question by editing it
Eoin Campbell
EdCenterList is pulled from a db and has a key. I think that is why it has a value. ___ new SelectList(new EdCenterRepository().GetAllEdCenters(1), "ID", "EdCenterName"); ___ the GetAllEdCenters function just makes a Linq2SQL call and returns an IQueryable<EdCenter> list
I replaced the Html.DropDownList with a foreach to write the <Select>, added a value tag, and it worked. Thanks!
The HTML spec states that, if no value attribute is supplied, the value is the option text. However, I've seen similar behavior that led me to conclude - just always supply the value. :)
GalacticCowboy
I've had this problem a couple of times but usually only in Internet Explorer. Firefox seems to be able to use the value of an option when no id is present. It would be nice if the helper would automatically set the id and value the same when no id is supplied.
Sam Wessel
A: 

Is there no solution to this? Seems like something is broken in the Ajax form submission.

Here is a good fix, write your methods as normal, then run:

 function addDropDownValues() {
            $("select option").each(function(i) {
                if ($(this).text() == "Red") { $(this).attr('value', "Red"); }
                else if ($(this).text() == "Amber") { $(this).attr('value', "Amber"); }
                else if ($(this).text() == "Green") { $(this).attr('value', "Green"); }
                else if ($(this).text() == "Complete") { $(this).attr('value', "Complete"); }
             });
        }

Where "red", "amber", etc... are the values you want to be passed through

This will append a value to each dropdown, producing:

<select name="status1" id="status1"><option value="Red">Red</option>
<option value="Amber">Amber</option>
<option selected="selected" value="Green">Green</option>
<option value="Complete">Complete</option>
</select>

This will work with both Firefox AND Internet Explorer. Internet Explorer requires the value item, firefox does not.

Daz