当我从下拉列表中选择一个选项时,如何更改2个文本框

时间:2016-09-29 08:56:34

标签: c# asp.net-mvc

http://i.stack.imgur.com/R4pFf.png

当我从下拉列表中选择选项时,我想更改两个文本框,但我不知道该怎么做。

我正在使用dbml文件连接到其他数据库。我想为2个文本框使用2个属性= STOK_FIYAT和STOK_KODU。

    private BETASYSEntities db = new BETASYSEntities();
    private NETSISDataContext netsisdb = new NETSISDataContext();     

    public ActionResult Olustur(int FormulID)
    {
        FormulasyonAyrintilari formulasyonayrintilari = new FormulasyonAyrintilari();
        formulasyonayrintilari.FormulID = FormulID;

        ViewBag.StokAdi = new SelectList(netsisdb.TBLSTSABIT, "STOK_ADI", "STOK_ADI");

        return PartialView("_Olustur", formulasyonayrintilari);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Olustur([Bind(Include = "FormulAyrintilariID,FormulID,StokAdi,StokKodu,StokBirim,StokMiktar,StokFiyat")] FormulasyonAyrintilari formulasyonayrintilari)
    {
        if (ModelState.IsValid)
        {
            db.FormulasyonAyrintilari.Add(formulasyonayrintilari);
            db.SaveChanges();

            string url = Url.Action("Index", "FormulasyonAyrintilari", new { id = formulasyonayrintilari.FormulID });
            return Json(new { success = true, url = url });

        }

        ViewBag.StokAdi = new SelectList(netsisdb.TBLSTSABIT, "STOK_ADI", "STOK_KODU", formulasyonayrintilari.StokAdi);

        return PartialView("_Olustur", formulasyonayrintilari);
    }

@Html.DropDownList("StokAdi", null, "Lütfen Seçin", htmlAttributes: new { @class = "form-control col-md-3 select2", autocomplete = "off" })
@Html.EditorFor(model => model.StokKodu, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
@Html.EditorFor(model => model.StokFiyat, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })

1 个答案:

答案 0 :(得分:0)

创建一个返回JsonResult的动作,它将接收值STOK_ADI并返回一个具有STOK_KODU和STOK_FIYAT的JSON对象

public JsonResult GetValuesForSTOK_ADI(string STOK_ADI)
{
 //get the values from the DB for the provided STOK_ADI  
return Json(new {STOK_KODU ="value from db",STOK_FIYAT="value from db"},JsonRequestBehavior.AllowGet));
}

在您的视图中,更改下拉列表的声明以添加将调用操作的onchange事件

@Html.DropDownList("StokAdi", null, "Lütfen Seçin", htmlAttributes: new { @class = "form-control col-md-3 select2", autocomplete = "off",onchange="updateTextBoxes()"})

在文件末尾,创建以下javascript函数

<script>
function updateTextBoxes()
{
  var selectedSTOK_ADI = $("#StokAdi").val();
  $.ajax('@Url.Action("GetValuesForSTOK_ADI","ControllerName")').then(function(data){
$("#StokKodu").val(data.STOK_KODU);
$("#StokFiyat").val(data.STOK_FIYAT);
});
}
</script>