views:

50

answers:

1

With the following code:

<script type="text/javascript" src="../../Scripts/jquery-1.4.1.js" />
<script type="text/javascript">
    $(function () {
        $('#selectOptions').change(function () {
            $(this).parents('form').submit();
        });
    });
</script>

...

    <% using (Html.BeginForm("PostedValue", "Home"))
   { %>

<%: Html.DropDownList("selectOptions", new List<SelectListItem> 
                                 { new SelectListItem{ Text="1",  Value="1"},  
                                   new SelectListItem{ Text="2",  Value="2"},  
                                   new SelectListItem{ Text="3",  Value="3"}},
                                 "Select List")%>
<%} %>
</div> </body>
</html>

What am I doing wrong?

A: 

when I do the following it works

<%: Html.DropDownList("selectOptions", new List { new SelectListItem{ Text="1", Value="1"},
new SelectListItem{ Text="2", Value="2"},
new SelectListItem{ Text="3", Value="3"}}, "Select List", new { onchange = "$(this).parents('form').submit()" })%> <%} %>

I am wondering why $function() is not working..

nitroxn
I don't know (and my answer's staying deleted now). When I copy and paste the code you provided (in the original question) into one of my own existing ASP.NET MVC 2 projects, it works like a champ. Didn't even change a character on it. :) **Did you actually copy-and-paste the question code, or might it have been paraphrased?** (Might there be another script screwing with things, or might there be -- hopefully not -- multiple elements with the `selectOptions` id, which may confuse the jQuery selection engine?)
John Rudy
Why is the script tag with the jquery not working whereas inline code is working perfectly.
nitroxn
I don't know -- are there multiple IDs of `selectOptions`? (Although that shouldn't cause a problem; the selector should just return the first one.) Is there potentially another script screwing things up? Are you sure in your actual code all the capitalization, etc, in the jQuery is correct? If you run the page through Firebug, do you see Javascript errors at any point? (Have you run the page through Firebug's Javascript debugger?)
John Rudy
Like I said, when I copied-and-pasted your code from the question, it just worked. Using the script tag with the jQuery, from the [question itself](http://stackoverflow.com/questions/3189585/asp-net-mvc-autopostback-drop-down-not-working).
John Rudy