Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am developing a MVC application .net.

I have a textbox called "Products". I have to autopoulate the textbox with the list from the server using jquery and ajax

Here is the code:

@Html.TextBoxFor(model=>model.Products,new {@id = "Products"})

In this textbox ,I need to call an action of a controller by ajax and get the data.

$('#Products).autocomplete()
{
    alert('kl');
    AutoCompleteDetails()
}

function AutoCompleteDetails()
{
    var url = '@Url.Action("Search", "Student")';
    href = url;

    $.ajax({
            url: href,
            dataType: "json",
            data: AutoDetails,
            type: "POST",
            context: this,
            success: function (data) {

}

On the page load, the control is hitting Search action of Student controller, I have two doubts here:

  1. On page load, after hitting "Search" action I am binding the model values back to the page but these values are not seen as dropdown as suggestions in the textbox called "Products"

  2. After the page load, when I type "KL" and tab out the action "Search" of "Student" controller is not getting called.

Any suggestions?

share|improve this question
Do you want the data to be populated as dropdown in the textbox after typing something in textbox? – user1907849 2 hours ago
Yes ,I need the data as dropdown – Jquery Developer 2 hours ago
Which version of jquery are you using? – user1400915 2 hours ago
<script src="code.jquery.com/jquery-1.9.1.js"; type="text/javascript"></script> <script src="code.jquery.com/ui/1.10.3/jquery-ui.js"; type="text/javascript"></script> – Jquery Developer 2 hours ago
Please fix the code in your question. There are some missing quotation marks, semi-colons, and brackets. I edited the formatting, but I couldn't touch the code, because it could be part of the problem. – ataravati 2 hours ago
add comment (requires an account with 50 reputation)

1 Answer

We have implemented same thing in our project.

Let me show you an example

  @Html.TextBoxFor(m => m.Filter.MyData)
                                <span>
                                    <img onclick=" openAutoCompleteBox() " alt="click" src="../Content/images/dropdown_arrow.gif"
                                        height="19px" style="cursor: pointer;"></span>

What I am doing in this is that I am trying to make it functional as Dropdown, so added an image at side of it , onclicking on it, it will call openAutoCompleteBox.

  1. On page load, after hitting "Search" action I am binding the model values back to the page but these values are not seen as dropdown as suggestions in the textbox called "Products"

For this what we have done is, we have loaded values at page load only and Autocomplete plugin will search value on client side only

  1. After the page load, when I type "KL" and tab out the action "Search" of "Student" controller is not getting called.

You will not need to call controller, as Autocomplete plugin will take care of this

I am not sure, if my implementation matches to your requirement but I calling controller again and again after a character will give your production server extra load. If you have static values then I would suggest it to load it at time when page loads. If its dynamic then your approach is fine.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.