?
Solved

JSON -- populate two textboxes after user SELECTION ?

Posted on 2013-12-17
2
Medium Priority
?
431 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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