Solved

JSON -- populate two textboxes after user SELECTION ?

Posted on 2013-12-17
2
424 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

705 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