Reputation: 1143
Hey.. How to receive selected Dropdownlist value in [HttpPost] method on change event? I always receive it in [HttpGet] method.
<%: Html.DropDownListFor(model => model.TipTpa, ViewData[ArtikliKonstante.vdListaTipovaTPa] as IEnumerable<SelectListItem>,
new { onchange = "location.href='/Artikli/PromjenaTipa? p='+this.value"})%>
If I declare my method as [HttpPost] I get error, that action doesn't exist. Any idea? Thx
Upvotes: 1
Views: 1836
Reputation: 1039498
You need to POST if you want the proper action to be invoked. In your case you are simply redirecting (window.location.href) which sends a GET request.
So you could place the dropdown inside a form and use javascript to submit the form when the selection changes:
<% using (Html.BeginForm("PromjenaTipa", "Artikli", FormMethod.Post, new { id = "myform" })) { %>
<%: Html.DropDownListFor(
model => model.TipTpa,
ViewData[ArtikliKonstante.vdListaTipovaTPa] as IEnumerable<SelectListItem>,
new { id = "tipTpa" }
) %>
<% } %>
and then subscribe for the change event in javascript (example with jquery):
$(function() {
$('tipTpa').change(function() {
$('#myform').submit();
});
});
This will POST the form to the PromjenaTipa
action where you could read the selected value:
[HttpPost]
public ActionResult PromjenaTipa(string tipTpa)
{
// tipTpa should contain the selected value
...
}
Another possibility would be to send an AJAX query in the change event. In this case you don't need a form as you could send POST request with AJAX.
Upvotes: 3