Solved

JSON -- populate two textboxes after user SELECTION ?

Posted on 2013-12-17
2
413 Views
Last Modified: 2013-12-19
Currently everything gets saved into
@Html.TextBox("productDescription")
which I do NOT want.

How can I change below code so step #4 populates
@Html.TextBox("productName") with selected c.PartNumber
and @Html.TextBox("productDescription") with its
associated c.PartDescription ?

Steps
 1. user enters data into @Html.TextBox("productName")
 2. search automatically populates @Html.TextBox("productName")
    with possible c.PartNumber --->  c.PartDescription) results
 3. user selects a results
 4. @Html.TextBox("productName") gets updated with
    selected c.PartNumber --->  c.PartDescription)
----------------------------------------------------
AutoCompleteController.cs
        public JsonResult getData(string term)
        {

            var mySearchResults = db.Lookup_PartNumbers
                                    .Where(c => c.PartNumber.Contains(term) || c.PartDescription.Contains(term))
                                    .Select(c => c.PartNumber + "  --->  " + c.PartDescription)
                                    .Take(10);

            return Json(mySearchResults, JsonRequestBehavior.AllowGet);
        }

Open in new window

--------------------------------------------------------------------------------------------------------
Index.cshtm
l
    <script type="text/javascript">
        $(function () {
            $('#productName').autocomplete({
                source: function (request, response) {
                    $.getJSON("/AutoComplete/getData?term=" + request.term, function (data) {
                        response(data);
                    });
                },
                minLength: 2,
                delay: 100
            });
        });
    </script>

Open in new window

               Product: @Html.TextBox("productName")
                Description: @Html.TextBox("productDescription")
0
Comment
Question by:finance_teacher
2 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39726088
What does response(data) do?
0
 
LVL 8

Accepted Solution

by:
vr6r earned 500 total points
ID: 39727767
Try replacing your Index.cshtm code with the following:

<script type="text/javascript">
	$(function () {
		$('#productName').autocomplete({
			source: function (request, response) {
				$.getJSON("/AutoComplete/getData?term=" + request.term, function (data) {
					response(data);
				});
			},
			select: function( event, ui ) {
				$('#productName').val(ui.item.value.split("  --->  ")[0]);
				$('#productDescription').val(ui.item.value.split("  --->  ")[1]);
				return false;
			},
			minLength: 2,
			delay: 100
		});
	});
</script>

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now