在asp.net mvc实体框架中使用javascript基于dropdownlist选择填充多个文本框
我正在使用Asp.NETMVC5和实体框架。这两种技术我都是新手 基本上我创建了一个表单,在这个表单中,当我从下拉列表中选择值时,可以使用下拉列表。我想填写文本框,此表格中也提供文本框 控制器:在asp.net mvc实体框架中使用javascript基于dropdownlist选择填充多个文本框,javascript,asp.net-mvc,entity-framework,Javascript,Asp.net Mvc,Entity Framework,我正在使用Asp.NETMVC5和实体框架。这两种技术我都是新手 基本上我创建了一个表单,在这个表单中,当我从下拉列表中选择值时,可以使用下拉列表。我想填写文本框,此表格中也提供文本框 控制器: public ActionResult Create() { var listrang = db.Ranglists.ToList(); ViewBag.listRangs = new SelectList(listrang, "RangId"
public ActionResult Create()
{
var listrang = db.Ranglists.ToList();
ViewBag.listRangs = new SelectList(listrang, "RangId", "Nom","Cour","td","tp");
return View();
}
public ActionResult Action(string Nom)
{
var query = from c in db.Ranglists where c.Nom == Nom select new {nom = c.Nom,cour = c.Cour, Td = c.td,Tp =c.tp };
return Json(query, JsonRequestBehavior.AllowGet);
}
razor视图:
<div class="form-group">
@Html.LabelFor(model => model.Rang, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Rang, ViewBag.listRangs as SelectList, "choose", htmlAttributes: new { onchange = "Action(this.value);",@class = "form-control" })
@Html.ValidationMessageFor(model => model.Rang, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Cours, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Cours, new { htmlAttributes = new { @class = "form-control", @id = "cour" } })
@Html.ValidationMessageFor(model => model.Cours, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tds, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tds, new { htmlAttributes = new { @class = "form-control", @id = "td" } })
@Html.ValidationMessageFor(model => model.Tds, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tps, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tps, new { htmlAttributes = new { @class = "form-control", @id = "tp" } })
@Html.ValidationMessageFor(model => model.Tps, "", new { @class = "text-danger" })
</div>
</div>
最后是函数Javascript
<script src="~/Scripts/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
function Action(nom) {
$.ajax({
url: '@Url.Action("Action", "Coordonnees")',
type: "POST",
data: { "nom": nom },
success: function (data) {
if (data != null) {
var vdata = data;
$("#cour").val(vdata[0].cour);
$("#td").val(vdata[0].Td);
$("#tp").val(vdata[0].Tp);
}
}
});
}
</script>
但不起作用