In Asp.net MVC, create a dynamic query using Linq and a drop down list chosen option.

asp.net-mvc dynamic-linq linq-to-sql

Question

I'm trying to accomplish this with Linq to Sql and Asp.Net MVC:

I have a Drop Down List with options Country, City and State. And beside it, there is a textbox. So, an user will, for instance, select City and will type "new york city" in the textbox and the application will show himm the results of his choice.

So, how can I make a dynamic query based on what the user selected in the drop down? I'm not trying to make something hardcoded, because I intend to use this query with some other things.

Thanks a lot!!

1
0
9/22/2009 12:43:06 PM

Popular Answer

It sounds like you are looking for an AJAX enabled auto complete textbox. Something similar to Ben Scheirman's blog article http://flux88.com/blog/jquery-auto-complete-text-box-with-asp-net-mvc/ would do the trick.

Using that article as a base for your code you would change the autocomplete javascript behavior to include the City/State/Country parameter in your call to the MVC controller.

<script type="text/javascript"> 
    $(document).ready(function() { 
    var dropdownValue = $("#DropDownListId").val();
    $("input#city").autocomplete('<%= Url.Action("Find", "City") %>' + dropdownValue + '/'); 
    }); 
</script>

From the Linq 2 SQL side of the application it is hard to give you specific guidance on how to accomplish this because we know nothing about the design of your database. But I would image the query would be fairly simple to write.

Does that answer your question?

1
9/22/2009 1:25:52 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow