Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello ,
Please i'm trying to fill the textbox (address) from dropdownlist (Name) selected Name Please Help me with this i'm stuck with problem since yesterday :(
Here's the code i wrote
Best Regards.

What I have tried:

My View 
------
<pre>   <div class="editor-label">
                                @Html.LabelFor(model => model.recieverName)
                                </div>
                                <div class="editor-field">
                         @Html.DropDownListFor(model => model.recieverName, listOfDealers,"-Select-", new { id = "name1", style = "width:180px;" })
                            
                                </div> 

<div class="editor-label">
                                  @Html.LabelFor(model => model.senderAddress)
                              </div>
                              <div class="editor-field">
                                  @Html.TextBoxFor(model => model.recieverAddress, new { id = "addresstxt", style = "width:180px; cursor:default;" })

                              </div>


<script type="text/javascript">


jQuery(document).ready(function () {
        $("#name1").change(function () {
            var ServiceUrl = "/Sheet/GetAddress?fname=" + fname;
            var content = '';
            $.support.cors = true;
            $.ajax({
                type: 'POST',
                url: ServiceUrl,
                async: true,
                cache: false,
                crossDomain: true,
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                error: function (xhr, err) {
                },
                success: function (query) {
                    $('#addresstxt').val(query.Value);

                }
            });
        });
          });


My conroller 
----------------

<pre>  [HttpGet]
        public JsonResult GetAddress(DealerModel dlr  , string fname  )
        {
       
            //According id to query the database and get the relevant values.

             using (TICK_DBEntities db = new TICK_DBEntities())
             {
                 
                 var query = db.TICK_Dealers.Where(c => c.fullName == fname).Select(c => new {Address = c.address, MobileNo = c.mobileNo }).FirstOrDefault();
                 return Json(query, JsonRequestBehavior.AllowGet);
             }
        }
<
Posted
Updated 28-Aug-18 3:23am
Comments
Richard Deeming 28-Aug-18 13:29pm    
var ServiceUrl = "/Sheet/GetAddress?fname=" + fname;

Where is that fname variable defined?

Use your browser's developer tools to monitor network (XHR) requests from your page, and see precisely what you're sending to the server, and what it's returning.
Muna Fadelallah 30-Aug-18 7:17am    
in conroller
[HttpGet]
public JsonResult GetAddress(DealerModel dlr , string fname )
{

//According id to query the database and get the relevant values.

using (TICK_DBEntities db = new TICK_DBEntities())
{

var query = db.TICK_Dealers.Where(c => c.fullName == fname).Select(c => new {Address = c.address, MobileNo = c.mobileNo }).FirstOrDefault();
return Json(query, JsonRequestBehavior.AllowGet);
}

Does your code throws an error? If so then what is it? Does your GetAddress action get hit when you set a break point while on debugging? Also what's the value of query.Value?

If you are returning a Model/Class then you should reference the property of the Model like this in your AJAX success callback:

JavaScript
success: function (query) {
                    $('#addresstxt').val(query.YourModelPropertyName);

 }


To make it more clear, for example you have the following Model/Class:
C#
public class Student
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
}


Now you want to return the Student model in your Action Controller:

C#
public JsonResult AjaxGetCall() {  
            Student student = new Student{  
                FirstName= "Vynn",  
                LastName= "Durano"
            };  
            return Json(student, JsonRequestBehavior.AllowGet);  
}  


To get the result from your AJAX request, you can do something like this:

JavaScript
$.ajax({  
            type: "GET",  
            url: "/YourControllerName/AjaxGetCall",  
            contentType: "application/json; charset=utf-8",  
            dataType: "json",  
            success: function(response) {  
                if (response != null) {  
                    $('#YourTextBoxID1').val(response.FirstName);  
                    $('#YourTextBoxID2').val(response.LastName);   
                } else {  
                    alert("object is null");  
                }  
            },  
            failure: function(response) {  
                alert(response.responseText);  
            },  
            error: function(response) {  
                alert(response.responseText);  
            }  
        });  
 
Share this answer
 
Comments
Muna Fadelallah 28-Aug-18 5:52am    
No it don't throws errors just no result ,
i tried it but didn't work either
Vincent Maverick Durano 28-Aug-18 9:01am    
Then you have to debug your code. Please note that we can't access your machine and do the debugging for you. We're here just to assist you and guide.

The solution that i've provided should help you get started on how to do that.
There are a couple of issues with your code from what I can see.

on your controller you are labelling it
[HttpGet]
so to access the code you must do a "get" in your JS you are doing $.ajax with a "Post" I would look at doing a $.get() (you can see that Vincent's answer has "Get" not "Post" in the $.ajax)

Ultimately using your browser's dev tools is your answer, the Network tab will tell you of any errors you are receiving.

In JS console.log() is useful for logging out variables.

I probably would use a Class rather than an anonymous in your db select
.Select(c => new<pre> {Address = c.address, MobileNo = c.mobileNo })

so it would be
.Select(c => new AddressClass = <pre> {Address = c.address, MobileNo = c.mobileNo })


An area where you will get some optimisation is rather than
var query = db.TICK_Dealers.Where(c => c.fullName == fname).Select(c => new {Address = c.address, MobileNo = c.mobileNo }).FirstOrDefault();

you should do
var query = db.TICK_Dealers.FirstOrDefault(c => c.fullName == fname).Select(c => new {Address = c.address, MobileNo = c.mobileNo });

or you even
<pre> var query = db.TICK_Dealers.Find(c => c.fullName == fname).Select(c => new {Address = c.address, MobileNo = c.mobileNo });
 
Share this answer
 
Comments
Richard Maly 29-Aug-18 2:16am    
Why the downvote?
Muna Fadelallah 29-Aug-18 7:51am    
i changed it thank u for the answer i'll try that
Muna Fadelallah 30-Aug-18 7:17am    
sorry but your solutions didn't work
 
Share this answer
 
Comments
Vincent Maverick Durano 27-Aug-18 12:10pm    
the question isn't about winforms. Please pay attention to the question carefully before throwing a proposed solution. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900