Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Mvc dropdown selected indexChanged load data from backend (sql server 2012) to textboxes.how to do that.

Model:
C#
public class QuotationModel
    {
 public string AccountNo { get; set; }

  public DataSet Account_No()
        {

            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString);
            SqlCommand cmd = new SqlCommand("Get_AccountNo", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds1 = new DataSet();
            da.Fill(ds1);
            return ds1;
        }
}

View:
HTML
<td>
                                        @Html.LabelFor(model => model.AccountNo, htmlAttributes: new { @class = "control-label col-md-2" })
                                    </td>
                                    <td>

                                        @Html.DropDownList("Account_No", (IEnumerable<SelectListItem>)ViewBag.Account_No, "select AccountNo", new { @class = "form-control", @id = "AccGroupDropDown", @onchange = "copyValue1()" })
                                    </td>

Controller:
C#
public class QuotationController : Controller
    {
        string connection = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        // GET: Quotation
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Quotation(QuotationModel QM)


        {

            //Sales_order_AccountNo
            DataSet ds1 = QM.Account_No();
            ViewBag.fname = ds1.Tables[0];
            List<SelectListItem> Account_No = new List<SelectListItem>();
            foreach (System.Data.DataRow dr in ViewBag.fname.Rows)
            {

                Account_No.Add(new SelectListItem { Text = @dr["Account_No"].ToString(), Value = @dr["Customer_Id"].ToString() });
            }


            ViewBag.Account_No = Account_No;
}
}


I want on the selection of account no. Customr name and address should be fetch from backend in texboxes.
where to write code for for that and how?

What I have tried:

Tried the above code.
Posted
Updated 9-May-16 21:56pm
v2

 
Share this answer
 
Unfortunately I can not write complete code for you neither anyone else will but I can tell you the steps to achieve what you need.
1. Create a method inside the controller with return type as JsonResult. Foe example:
C#
public JsonResult GetValuesFromDatabase(int inputId)

2. Perform Database query in the method and return something like:
C#
public JsonResult GetValuesFromDatabase(int inputId){
//Perform database query here and assign that value to the variables to be returned.
var firstValue = "Value for TextBox1"; //Get this from database;
var secondValue = "Value for TextBox2"; //Get this from database;
var jsonData = new
            {
                value1: firstValue,
                value2: secondValue 
            };
return Json(jsonData, JsonRequestBehavior.AllowGet);
}

3. Since you have already written "onchange" event for your dropdownlist, you will have to implement that function somehere on your view(a seperate JS file would be better). For example:
JavaScript
function copyValue1(){
 $.ajax({
        type: "GET",
        url: "/YourControllerName/GetValuesFromDatabase",//Change the function name with your actual function name
        dataType: "Json",
        data: { inputId: $("#YourDropdownList").val()},
        success: function (response) {
//assign values to your textboxes.
$("#Textbox1").val(response.value1);
$("#Textbox2").val(response.value1);
}
});
}


Hope it helps.
 
Share this answer
 
Comments
Member 12385326 12-May-16 2:02am    
@zafar sultan thnx for your help.can you give me example of database query.i mean how to write it in mvc.where you have written "value for textbox1" and "value for textbox2"

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