Mvc下拉选择indexchanged [英] Mvc dropdown selected indexchanged
问题描述
Mvc下拉选择indexChanged从后端(sql server 2012)加载数据到textboxes.how来做到这一点。
型号:
Mvc dropdown selected indexChanged load data from backend (sql server 2012) to textboxes.how to do that.
Model:
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:
<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:
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;
}
}
我想选择帐户号码。自定义名称和地址应该从texbox中的后端获取。
在哪里为其编写代码以及如何编写代码?
什么我试过了:
试过上面的代码。
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.
推荐答案
你好,
参考: ASP.NET MVC中的DropDownList和Postback
不幸的是,我不能为你编写完整的代码,但我无法告诉你实现你需要的步骤。
1.在控制器内创建一个方法,返回类型为JsonResult。敌人示例:
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:
public JsonResult GetValuesFromDatabase(int inputId)
2.在方法中执行数据库查询并返回如下内容:
2. Perform Database query in the method and return something like:
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.由于您已经为下拉列表编写了onchange事件,因此您必须在视图中的某个位置实现该功能(单独的JS文件会更好)。例如:
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:
function copyValue1(){
。 ajax({
type: GET,
url: / YourControllerName / GetValuesFromDatabase, // 使用实际函数名称更改函数名称
dataType: Json,
数据:{inputId:
.ajax({ type: "GET", url: "/YourControllerName/GetValuesFromDatabase",//Change the function name with your actual function name dataType: "Json", data: { inputId:
这篇关于Mvc下拉选择indexchanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!