Solved

JSON -- populate two textboxes after user SELECTION ?

Posted on 2013-12-17
2
420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

740 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