This is not as easy to do as you might think, since from the html point of view, only the value selected in the drop-down list is the value sent to the server in submit.
, json jquery, .
:
,
public ActionResult MyAction(string myDropDown, string myDropDownValues)
Ajax,
@using (Ajax.BeginForm("MyAction", new AjaxOptions()))
{
@Html.Hidden("myDropDownValues")
<select name="myDropDown" id="myDropDown">
<option value="0">First</option>
<option value="1">Second</option>
<option value="2">Third</option>
<option value="3">Fourth</option>
<option value="4">Fifth</option>
</select>
<input type="submit" id="submit" />
}
submit : $('form').submit(function(){});
, , . .each(), json .
json, , .
:
<script>
$(document).ready(function () {
$('form').submit(function () {
var json = "";
$("#myDropDown option").each(function () {
value = $(this).val()
text = $(this).html()
if (json.length != 0) json += ",";
json += value + ":" + text;
});
$('#myDropDownValues').val(json.toString());
});
});
</script>
, myDropDownValues :
0:First,1:Second,2:Third,3:Fourth,4:Fifth