英文:
ASP.NET Core MVC create cascading dropdown lists from single table (Razor)
问题
我首先填充IdDegree
下拉列表,根据选择填充DegreeTitle
下拉列表。例如,对于IdDegree = 2
,列表中有2个选项以及多个DegreeTitle
(test1,test2,...,test5)的数据。因此,当用户选择IdDegree = 2
时,DegreeTitle
下拉列表应该被填充为test1,test2,...test5。
SQL数据库
ID_Degree | Degree_Title |
---|---|
spic | manger |
1 | Exm1 |
1 | Exm2 |
1 | Exm3 |
2 | Test1 |
2 | Test2 |
2 | Test3 |
2 | Test4 |
2 | Test5 |
模型类:
public partial class DegreeName
{
public int Id { get; set; }
public string IdDegree { get; set; }
public string DegreeTitle { get; set; }
public string DegreeName1 { get; set; }
}
控制器:
public async Task<IActionResult> Create([Bind("Id,IdDegree,DegreeTitle,DegreeName1")] DegreeName degreeName)
{
if (ModelState.IsValid)
{
// List<DegreeName> IdDegree = new List<DegreeName>;
ViewBag.IdDegree = db.DegreeName.ToList();
return View();
}
db.Add(degreeName);
await db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
return View(degreeName);
}
这是Razor视图:
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="IdDegree" class="control-label"></label>
<input asp-for="IdDegree" class="form-control" />
<span asp-validation-for="IdDegree" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DegreeTitle" class="control-label"></label>
<input asp-for="DegreeTitle" class="form-control" />
<span asp-validation-for="DegreeTitle" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DegreeName1" class="control-label"></label>
<input asp-for="DegreeName1" class="form-control" />
<span asp-validation-for="DegreeName1" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
很多YouTube视频都没有解决我的问题,涉及单表级联。
英文:
I'm first filling IdDegree
dropdownlist, based on the selection fills the DegreeTitle
dropdownlist. For example for IdDegree = 2
, there are 2 in list and multiple DegreeTitle
(test1, test2,..., test5) of data. So when the user selects IdDegree = 2
, the DegreeTitle
dropdownlist should be populated with test1, test2,...test5.
SQL database
ID_Degree | Degree_Title |
---|---|
spic | manger |
1 | Exm1 |
1 | Exm2 |
1 | Exm3 |
2 | Test1 |
2 | Test2 |
2 | Test3 |
2 | Test4 |
2 | Test5 |
Model class:
public partial class DegreeName
{
public int Id { get; set; }
public string IdDegree { get; set; }
public string DegreeTitle { get; set; }
public string DegreeName1 { get; set; }
}
Controller:
public async Task<IActionResult> Create([Bind("Id,IdDegree,DegreeTitle,DegreeName1")] DegreeName degreeName)
{
if (ModelState.IsValid)
{
// List<DegreeName> IdDegree = new List<DegreeName>;
ViewBag.IdDegree = db.DegreeName.ToList();
return View();
}
db.Add(degreeName);
await db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
return View(degreeName);
}
This is the Razor view:
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="IdDegree" class="control-label"></label>
<input asp-for="IdDegree" class="form-control" />
<span asp-validation-for="IdDegree" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DegreeTitle" class="control-label"></label>
<input asp-for="DegreeTitle" class="form-control" />
<span asp-validation-for="DegreeTitle" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DegreeName1" class="control-label"></label>
<input asp-for="DegreeName1" class="form-control" />
<span asp-validation-for="DegreeName1" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
A lot of YouTube video and didn't fix my problem with single table cascading
答案1
得分: 0
从您的问题中,我认为当您创建DegreeName
项时,您想要使用级联下拉列表来填充IdDegree
和DegreeTitle
。我创建了一个演示来展示如何实现它,希望它可以帮助您解决问题。
我创建了一个模型来保存您想要在下拉列表中选择的值
public class DropDownData
{
public int Id { get; set; }
public string IdDegree { get; set; }
public string DegreeTitle { get; set; }
}
控制器动作:
public IActionResult CreateDegreeName()
{
var data = _context.dropDownDatas.Select(x => x.IdDegree).Distinct().ToList();
var selectlist = new List<SelectListItem>();
var selectlistTitle = new List<SelectListItem>();
foreach (var item in data)
{
selectlist.Add(new SelectListItem() { Text = item, Value = item });
}
ViewBag.selectlist = selectlist;
return View();
}
[HttpPost]
public IActionResult CreateDegreeName(DegreeName model)
{
//..........将DegreeName保存到数据库中....
//..............
}
public IActionResult addItem(string IdDegree)
{
var title = _context.dropDownDatas.Where(y => y.IdDegree == IdDegree).Select(x => x.DegreeTitle).ToList();
return Json(title);
}
视图:
@model DegreeName
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="IdDegree" class="control-label"></label>
<select asp-for="IdDegree" asp-items="@ViewBag.selectlist" class="form-control" onchange="Test(this)">
<option value="">请选择 IdDegree</option>
</select>
<span asp-validation-for="IdDegree" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DegreeTitle" class="control-label"></label>
<select asp-for="DegreeTitle" class="form-control" disabled></select>
<span asp-validation-for="DegreeTitle" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DegreeName1" class="control-label"></label>
<input asp-for="DegreeName1" class="form-control" />
<span asp-validation-for="DegreeName1" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="创建" class="btn btn-primary" />
</div>
</form>
@section Scripts{
<script>
function Test(a){
var postData = {
'IdDegree': a.value,
};
var items = '';
$.ajax({
type: 'Get',
url: '/Test/addItem/',
data: postData,
contentType: "application/json",
success: function (data) {
$('#DegreeTitle').prop('disabled', false);
for (var i = 0; i < data.length; i++){
items += "<option value = '" + data[i] + "'>" + data[i] + " </option>";
}
$('#DegreeTitle').html(items);
},
})
}
</script>
}
演示:
英文:
From your question, I think when you create DegreeName
item, you want to use cascading dropdown lists to fill IdDegree
and DegreeTitle
. I create a demo to show how to achieve it, hope it can help you solve the issue.
I create Model to save the value which you want to select in dropdownlist
public class DropDownData
{
public int Id { get; set; }
public string IdDegree { get; set; }
public string DegreeTitle { get; set; }
}
Controller Action:
public IActionResult CreateDegreeName()
{
var data = _context.dropDownDatas.Select(x=>x.IdDegree).Distinct().ToList();
var selectlist = new List<SelectListItem>();
var selectlistTitle = new List<SelectListItem>();
foreach (var item in data)
{
selectlist.Add(new SelectListItem() { Text = item, Value = item });
}
ViewBag.selectlist = selectlist;
return View();
}
[HttpPost]
public IActionResult CreateDegreeName(DegreeName model)
{
//..........save DegreeName into database....
//..............
}
public IActionResult addItem(string IdDegree)
{
var title = _context.dropDownDatas.Where(y => y.IdDegree == IdDegree).Select(x => x.DegreeTitle).ToList();
return Json(title);
}
View
@model DegreeName
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="IdDegree" class="control-label"></label>
<select asp-for="IdDegree" asp-items="@ViewBag.selectlist" class="form-control" onchange="Test(this)">
<option value="">Please Select IdDegree</option>
</select>
<span asp-validation-for="IdDegree" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DegreeTitle" class="control-label"></label>
<select asp-for="DegreeTitle" class="form-control" disabled></select>
<span asp-validation-for="DegreeTitle" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DegreeName1" class="control-label"></label>
<input asp-for="DegreeName1" class="form-control" />
<span asp-validation-for="DegreeName1" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
@section Scripts{
<script>
function Test(a){
var postData = {
'IdDegree': a.value,
};
var items = '';
$.ajax({
type: 'Get',
url: '/Test/addItem/',
data: postData,
contentType: "application/json",
success: function (data) {
$('#DegreeTitle').prop('disabled', false);
for (var i = 0; i < data.length; i++){
items += "<option value = '" + data[i] + "'>" + data[i] + " </option>";
}
$('#DegreeTitle').html(items);
},
})
}
</script>
}
Demo:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论