如何使用ajax使用实体框架在ASP.NET mvc5中按下拉列表填充表单字段 [英] How to populate a form field by dropdownlist in ASP.NET mvc5 with entity framework using ajax
问题描述
我在选择'TeacherId'DropDownList项后尝试自动填充CreditTobeTakenTextBox。我在数据库中有Teacher Table,其中TeacherId是主键,并尝试检索特定教师的CreditToBeTaken。但它不起作用。我在codeproject中看到了类似的示例,但它们并不适用于我。我认为我的ajax调用存在问题。请有人帮助我。
我尝试过的事情:
行动:
public JsonResult CreditToBeTaken(int id)
{
db .Configuration.ProxyCreationEnabled = false;
string credit =从Teacher选择CreditToBeTaken,其中TeacherId ='+ id +';
var creditsToBeTaken = db.Teachers.SqlQuery (信用);
返回Json(creditsToBeTaken);
}
查看:
@ Html.LabelFor(model => model.TeacherId,TeacherId,htmlAttributes:new {@class =control-label col -md-2})
@ Html.DropDownList(TeacherId,新的SelectList(string.Empty) ,价值,文字),请选择一位教师,htmlAttributes:new {@class =form-control})
@ Html.ValidationM essageFor(model => model.TeacherId,,new {@class =text-danger})
@ Html.LabelFor(model => model.CreditToBeTaken,htmlAttributes:新{@class =control-label col-md-2})
@ Html.EditorFor( model => model.CreditToBeTaken,new {@id =creditTobeTakenTextBox,@ class =form-control}
@ Html.ValidationMessageFor(model => model.CreditToBeTaken,,新{@class =text-danger})
脚本文件:
< script>
$ (文件).ready(function(){
$(#teacherId)。change(function(){
$ .ajax({
url:'@ Url.Action(CreditTo BeTaken)',
类型:'POST',
dataType:'json',
数据:{teacherID:$(# TeacherId)。val()},
contentType:'application / json; charset = utf-8',
成功:函数(数据){
if(data.success){
//填充信用要采取
$(#CreditToBeTakenTextBox)。val(data.creditsToBeTaken);
}
else {
//在警告或div中显示消息
alert('此教师ID无效。请重试!');
}
}
});
});
});
< / script>
(文件).ready(function( ){
(#teacherId)。change(function(){
.ajax({
url:'@ Url.Action (CreditToBeTaken)',
类型:'POST',
dataType:'json',
数据:{teacherID:
I am trying to populate CreditTobeTakenTextBox automatically after selecting a 'TeacherId' DropDownList item. I have Teacher Table in database where TeacherId is a primary key, and trying to retrieve CreditToBeTaken of a particular teacher. But it's not working. I have seen similar type of example in codeproject but they are not working for me. I think there is a problem in my ajax call. Please someone help me.
What I have tried:
Action:
public JsonResult CreditToBeTaken(int id)
{
db.Configuration.ProxyCreationEnabled = false;
string credit = "select CreditToBeTaken from Teacher where TeacherId='"+id+"'";
var creditsToBeTaken = db.Teachers.SqlQuery(credit);
return Json(creditsToBeTaken);
}
View:
@Html.LabelFor(model => model.TeacherId, "TeacherId", htmlAttributes: new { @class = "control-label col-md-2" })
@Html.DropDownList("TeacherId", new SelectList(string.Empty, "Value", "Text"), "Please select a Teacher", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TeacherId, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.CreditToBeTaken, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.CreditToBeTaken, new { @id = "creditTobeTakenTextBox", @class = "form-control" }
@Html.ValidationMessageFor(model => model.CreditToBeTaken, "", new { @class = "text-danger" })
Script File:
<script>
$(document).ready(function () {
$("#TeacherId").change(function () {
$.ajax({
url: '@Url.Action("CreditToBeTaken")',
type: 'POST',
dataType: 'json',
data: { teacherID: $("#TeacherId").val() },
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data.success) {
// fill the Credit to be taken
$("#CreditToBeTakenTextBox").val(data.creditsToBeTaken);
}
else {
// show a message in a alert or div
alert('This Teacher ID is not valid. Try again!');
}
}
});
});
});
</script>
(document).ready(function () {
("#TeacherId").change(function () {
.ajax({
url: '@Url.Action("CreditToBeTaken")',
type: 'POST',
dataType: 'json',
data: { teacherID:
这篇关于如何使用ajax使用实体框架在ASP.NET mvc5中按下拉列表填充表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!