在 MVC 中从 Azure 数据库填充 DropDownList
Populate DropDownList from Azure Database in MVC
我对 MVC 还是陌生的,并且正在努力解决它。我需要将我的学区 table 的 "name" 列放入下拉列表中,以便能够从不同的学区中进行选择。最终游戏是用户将从下拉列表中选择一个地区,然后被定向到一个页面,其中将显示所选地区的学校列表(在不同的 table 中)(我认为这将是一个使用下拉列表中给定的值查询数据库)。到目前为止我所做的基本上是:
创建 MVC 应用程序。
创建一个 Entity Framework 模型。
- 创建一个空控制器。
- 创建一个视图模型(因为每个教程/网站的答案都说过要做
所以)
- 创建视图。
我逐步复制了这些教程告诉我要做的事情,但我得到了不同的结果。我的下拉列表给了我这个结果:
我需要帮助来找出可能出错的地方以及为什么数据没有显示在我的下拉列表中。
尝试一下,我相信您使用了错误的 SelectList 构造函数。假设您希望下拉列表的值为 "leaID" 属性
@Html.DropDownList("myList", new SelectList(ViewBag.districts, "leaId", "name")
但是,我会以另一种方式处理它,这将使它主要保持强类型:
public class DistrictViewModel
{
public string SelectedDistrictId { get; set; }
public IEnumerable<SelectListItem> Districts { get; set; }
}
操作:
public ActionResult Index()
{
var viewModel = new DistrictViewModel()
{
Districts = new SelectList(db.Districts.ToList(), "leaID", "name")
}
return View(viewModel);
}
cshtml:
@model DistrictViewModel
@Html.DropDownListFor(m => m.SelectedDistrictId, Model.Districts)
这是我使用 ajax
对您的评论的回答
//Model
public class DistrictViewModel
{
public string name {get;set;}
public SelectList District {get;set;}
public int SelectedDistrict {get;set}
}
//Controller
public class DistrictController : Controller
{
KUDEREntities db = new KUDEREntities();
public ActionResult Index()
{
var model = new DistrictViewModel();
model.Districts = db.Districts.ToList();
model.SelectedDistrict=0;
return view(model);
}
[HttpPost]
public ActionResult Search(int id)
{
//do the search with the id of the selected district
var data = db.Districts.Where(m=>m.leaId = id).FirstorDefault();//this would return the whole object.
return Json(data, JsonRequestBehavior.AllowGet);
}
}
//Index View
@Model DistrictViewModel
<div>
//with this your selector would be "#SelectedDistrict"
@Html.DropDownListFor(model=>model.SelectedDistrict, new SelectList(Model.Districts,"leaId","name",Model.SelectedDistrict), new {@class=""})
//with this your selector would be "#myList"
//@Html.DropDownList("myList", new SelectList(ViewBag.districts, "leaId", "name", Model.SelectedDistrict)
</div>
<script>
$(document).ready(function(){
//when you want to search for the id selected, you just need to call the Search function below
function Search() {
//... now you have the value of the selecteditem
var parameters = { id: $("#SelectedDistrict").val() };
$.ajax({
type: 'post',
url: '@Url.Action("Search", "District")',
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "html",
async: true,
data: JSON.stringify(parameters),
success: function (data) {
//...do whatever with the data
},
failure: function (msg) {
//... show a message of error
}
});
}
});
</script>
我对 MVC 还是陌生的,并且正在努力解决它。我需要将我的学区 table 的 "name" 列放入下拉列表中,以便能够从不同的学区中进行选择。最终游戏是用户将从下拉列表中选择一个地区,然后被定向到一个页面,其中将显示所选地区的学校列表(在不同的 table 中)(我认为这将是一个使用下拉列表中给定的值查询数据库)。到目前为止我所做的基本上是:
创建 MVC 应用程序。
创建一个 Entity Framework 模型。
- 创建一个空控制器。
- 创建一个视图模型(因为每个教程/网站的答案都说过要做 所以)
- 创建视图。
我逐步复制了这些教程告诉我要做的事情,但我得到了不同的结果。我的下拉列表给了我这个结果:
我需要帮助来找出可能出错的地方以及为什么数据没有显示在我的下拉列表中。
尝试一下,我相信您使用了错误的 SelectList 构造函数。假设您希望下拉列表的值为 "leaID" 属性
@Html.DropDownList("myList", new SelectList(ViewBag.districts, "leaId", "name")
但是,我会以另一种方式处理它,这将使它主要保持强类型:
public class DistrictViewModel
{
public string SelectedDistrictId { get; set; }
public IEnumerable<SelectListItem> Districts { get; set; }
}
操作:
public ActionResult Index()
{
var viewModel = new DistrictViewModel()
{
Districts = new SelectList(db.Districts.ToList(), "leaID", "name")
}
return View(viewModel);
}
cshtml:
@model DistrictViewModel
@Html.DropDownListFor(m => m.SelectedDistrictId, Model.Districts)
这是我使用 ajax
对您的评论的回答//Model
public class DistrictViewModel
{
public string name {get;set;}
public SelectList District {get;set;}
public int SelectedDistrict {get;set}
}
//Controller
public class DistrictController : Controller
{
KUDEREntities db = new KUDEREntities();
public ActionResult Index()
{
var model = new DistrictViewModel();
model.Districts = db.Districts.ToList();
model.SelectedDistrict=0;
return view(model);
}
[HttpPost]
public ActionResult Search(int id)
{
//do the search with the id of the selected district
var data = db.Districts.Where(m=>m.leaId = id).FirstorDefault();//this would return the whole object.
return Json(data, JsonRequestBehavior.AllowGet);
}
}
//Index View
@Model DistrictViewModel
<div>
//with this your selector would be "#SelectedDistrict"
@Html.DropDownListFor(model=>model.SelectedDistrict, new SelectList(Model.Districts,"leaId","name",Model.SelectedDistrict), new {@class=""})
//with this your selector would be "#myList"
//@Html.DropDownList("myList", new SelectList(ViewBag.districts, "leaId", "name", Model.SelectedDistrict)
</div>
<script>
$(document).ready(function(){
//when you want to search for the id selected, you just need to call the Search function below
function Search() {
//... now you have the value of the selecteditem
var parameters = { id: $("#SelectedDistrict").val() };
$.ajax({
type: 'post',
url: '@Url.Action("Search", "District")',
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "html",
async: true,
data: JSON.stringify(parameters),
success: function (data) {
//...do whatever with the data
},
failure: function (msg) {
//... show a message of error
}
});
}
});
</script>