MVC Ajax does not post dropdownlist value
Using the following view in the 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 am trying to catch the values ββusing the code in my controller
public ActionResult PatientSearch(string patientType, int edCenterID) {
//do something with values
}
patientType is always passed as ""; however edCenterID is sent and only gets a penalty.
If I change this from Ajax.BeginForm to HTML.BeginForm everything works fine.
Is the problem in my DropDownList, my controller, and both?
ADD FROM COMMENTS
@Eoin pointed out to me that my choice is to render without value and that is probably the problem. This begs two questions:
1. Why does it work with a standard message, but not with an Ajax message.
2. How to get a list containing a combo box is a simple list of strings (no key). I am posting the code used to create my SelectList below
ViewData["PatientTypeList"]=new SelectList(new List<string>()
{ "Referral", "Patient"});
Whether the list of patient types is .Value
By the looks of it, theres no value
rendered for PatientType DDL <option>
, so essentially every parameter has value = "", so it actually returns the correct value.
The problem is what gets done first.
change
Try
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("referral", "referral");
d.Add("patient", "patient");
ViewData["PatientTypeList"]=new SelectList(d, "Key", "Value");
a source to share
Is there a solution? There seems to be something broken in the Ajax form submission.
Here's a good fix, write your methods as usual, 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 is "red", "amber", etc. are the values ββyou want to pass through
This will add a value for each dropdown, creating:
<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 a value element, firefox does not.
a source to share