In my view, I have a dropdown which I fill via Ajax call. This dropdown is within the form. Yet on submit, I don't see this control in the formCollection.
One more thing, alternatively when I try to add Html.DropDownList("AccountId"), I get error - There is no ViewData item of type 'IEnumerable' that has the key 'AccountId'.
Listed my View and controller code...
--View--
using (Html.BeginForm("GetNames", "Account", FormMethod.Post, new { id = "accountParameters" }))
{
....
....
<select id="AccountId" runat="server"></select> //This is not available in formcollection
//Html.DropDownList("AccountId"); //This throws exception
@:<p><input type='submit' value='Submit'/></p>
}
...
...
<script>
$(document).ready(function () {
$.ajax({
url: '/Account/GetAccounts',
type: "GET",
success: function (result) {
for (i = 0; i < result.length; i++) {
$('#AccountId').append($('<option></option>').val(result[i].accountId).html(result[i].name));
}
}
});
});
</script>
-- Controller --
public ActionResult GetAccounts(string id)
{
return Json(GetAccounts(), JsonRequestBehavior.AllowGet);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetNames(FormCollection formCollection)
{
if (("AccountId") != null)
{
....
....
}
}