таблица p_id p_name p_amount 1 телефонный счет 500 2 Продуктовых магазина 1000 3 Туалетные принадлежности 800 4 Спортзал 1500 5 Алкоголь 1800 6 Одежда 5000 7 мов ie 500
'' ' Контроллер
public ActionResult Product_Det()
{
Product_det Name = new Product_det();
Name.ProductName = ProductName();
return View(Name);
}
public JsonResult selectSBU(string id)
{
List<Product_det> branches = PopulateBranches();
return Json(branches.Where(x => x.p_id == id), JsonRequestBehavior.AllowGet);
}
Метод для текстового поля (p_amount)
private List<Product_det> PopulateBranches()
{
List<Product_det> items = new List<Product_det>();
string constr = ConfigurationManager.ConnectionStrings["localdb"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "select p_amount from tbl_product";
SqlCommand cmd = new SqlCommand(query);
cmd.Connection = con;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
var employee = new Product_det();
employee.p_amount = sdr["p_amount"].ToString();
}
}
return items;
}
Метод для раскрывающегося списка (p_name)
private static List<SelectListItem> ProductName()
{
List<SelectListItem> items = new List<SelectListItem>();
string constr = ConfigurationManager.ConnectionStrings["localdb"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = " select * from tbl_product";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new SelectListItem
{
Text = sdr["p_name"].ToString(),
Value = sdr["p_id"].ToString()
});
}
}
con.Close();
}
}
return items;
}
Просмотр
@model Expense_Tracker.Models.Product_det
@{
Layout = null;
}
<div id="main-div">
<div id="box" class="img-responsive">
@using (@Html.BeginForm())
{
<div class="insidebox">
<div> <h1>Product & Amount</h1></div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-user"></span></div>
@Html.DropDownList("p_name", new SelectList(ViewBag.Product_det, "p_name", "p_name"), "Select Product", new { @id = "ddlProduct_det", @class = "SelectCtrlGHN", onchange = "return get_sbu(this)" })
<label class="FieldGH">Price</label>
<input type="text" id="txtp_amount" name="GrpHead" value="" readonly class="PO_GH" />
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function get_sbu() {
var selected_val = $('#ddlProduct_det').find(":selected").attr('value');
$.ajax({
type: "POST",
url: "/Expense/selectSBU",
data: "id=" + selected_val,
success: function (data) {
if (data.length > 0) {
$('#txtp_amount').val(data[0].p_amount);
}
else {
$('#txtp_amount').val('');
}
}
});
}
</script>
<div class="form-group">
<input type="submit" value="Add" class="btn btn-success btn-block" />
</div>
<div class="form-group">
<p style="color:red">@ViewBag.error</p>
</div>
</div>
}
</div>
</div>
'' '