views:

399

answers:

2

Hi all, can anyone please help me about how to invoke a post method on changing the drop down list selection? I have Index method in my controller which takes me to index page. There i have placed a dropdown list using Html.Dropdown containing items like 1,2,3,etc. I want to call post method for Index whenever i makes any selection from the dropdown.

regards, kapil

A: 

Hey,

Are you using JQuery? You could use $.post to post your data in the onchange event... I think in the web forms way it was using a _doPostBack call in the onchange event if you set AutoPostBack to true... not 100% sure about that.

HTH

Brian
Thanks Brian.I am using Javascript to handle onchange event. How can i do so there?
kapil
try doing _doPostBack('<Select element name>', '<command>') and see if that works. That is a part of the MS Ajax framework and so you need to reference that.
Brian
+1  A: 

You can do this using JQuery

 <select id="idXYZ" name="XYZ" onchange="SetUnit(this.value)">
 <%=  ViewData["YourValue"]%>
</select>

Now use JQuery

<script type="text/javascript">
function SetUnit(unit) {
        $.post("/Admin/YourMethod?itemid=" + unit.toString(), function(data) {
            var data = eval('(' + data + ')');
            $("#idunit").html(data.Value);
        });
    }

</script>
Pankaj
If you are not using Jquery then use Ajax post method directly//POST http.open("POST", url, true);
Pankaj
Thanks Pankaj,but instead of <select>, i have used <%=Html.DropDownList...>.Then i did the same as you have said above. But unfortunately it's not working for me.
kapil
@@Kapil you are not using JQuey that's why your $.post is not working.Please use ajax post. see this article to use ajax posthttp://viralpatel.net/blogs/2008/12/ajax-post-method-example-using-javascript-jquery.html
Pankaj