ASP.NET Core MVC从单个表格创建级联下拉列表(Razor)

huangapple go评论83阅读模式
英文:

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&lt;IActionResult&gt; Create([Bind(&quot;Id,IdDegree,DegreeTitle,DegreeName1&quot;)] DegreeName degreeName)
{
    if (ModelState.IsValid)
    {
        // List&lt;DegreeName&gt; IdDegree = new List&lt;DegreeName&gt;;

        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:

&lt;form asp-action=&quot;Create&quot;&gt;
    &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;form-group&quot;&gt;
        &lt;label asp-for=&quot;IdDegree&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
        &lt;input asp-for=&quot;IdDegree&quot; class=&quot;form-control&quot; /&gt;
        &lt;span asp-validation-for=&quot;IdDegree&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
    &lt;/div&gt;

    &lt;div class=&quot;form-group&quot;&gt;
        &lt;label asp-for=&quot;DegreeTitle&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
        &lt;input asp-for=&quot;DegreeTitle&quot; class=&quot;form-control&quot; /&gt;
        &lt;span asp-validation-for=&quot;DegreeTitle&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
    &lt;/div&gt;

    &lt;div class=&quot;form-group&quot;&gt;
        &lt;label asp-for=&quot;DegreeName1&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
        &lt;input asp-for=&quot;DegreeName1&quot; class=&quot;form-control&quot; /&gt;
        &lt;span asp-validation-for=&quot;DegreeName1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
    &lt;/div&gt;

    &lt;div class=&quot;form-group&quot;&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-primary&quot; /&gt;
    &lt;/div&gt;    
&lt;/form&gt;

A lot of YouTube video and didn't fix my problem with single table cascading

答案1

得分: 0

从您的问题中,我认为当您创建DegreeName项时,您想要使用级联下拉列表来填充IdDegreeDegreeTitle。我创建了一个演示来展示如何实现它,希望它可以帮助您解决问题。

我创建了一个模型来保存您想要在下拉列表中选择的值

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>
}

演示:

ASP.NET Core MVC从单个表格创建级联下拉列表(Razor)

英文:

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; }
}

ASP.NET Core MVC从单个表格创建级联下拉列表(Razor)

Controller Action:

public IActionResult CreateDegreeName()
{
var data = _context.dropDownDatas.Select(x=&gt;x.IdDegree).Distinct().ToList();
var selectlist = new List&lt;SelectListItem&gt;();
var selectlistTitle = new List&lt;SelectListItem&gt;();
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 =&gt; y.IdDegree == IdDegree).Select(x =&gt; x.DegreeTitle).ToList();
return Json(title);
}

View

@model DegreeName
&lt;form  method=&quot;post&quot;&gt;
&lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label asp-for=&quot;IdDegree&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
&lt;select asp-for=&quot;IdDegree&quot; asp-items=&quot;@ViewBag.selectlist&quot; class=&quot;form-control&quot; onchange=&quot;Test(this)&quot;&gt;
&lt;option value=&quot;&quot;&gt;Please Select IdDegree&lt;/option&gt;
&lt;/select&gt;
&lt;span asp-validation-for=&quot;IdDegree&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label asp-for=&quot;DegreeTitle&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
&lt;select asp-for=&quot;DegreeTitle&quot;  class=&quot;form-control&quot; disabled&gt;&lt;/select&gt;
&lt;span asp-validation-for=&quot;DegreeTitle&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label asp-for=&quot;DegreeName1&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
&lt;input asp-for=&quot;DegreeName1&quot; class=&quot;form-control&quot; /&gt;
&lt;span asp-validation-for=&quot;DegreeName1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-primary&quot; /&gt;
&lt;/div&gt;
&lt;/form&gt;
@section Scripts{
&lt;script&gt;
function Test(a){
var postData = {
&#39;IdDegree&#39;: a.value,
};
var items = &#39;&#39;;
$.ajax({
type: &#39;Get&#39;,
url: &#39;/Test/addItem/&#39;,
data: postData,
contentType: &quot;application/json&quot;,
success: function (data) {
$(&#39;#DegreeTitle&#39;).prop(&#39;disabled&#39;, false);
for (var i = 0; i &lt; data.length; i++){
items += &quot;&lt;option value = &#39;&quot; + data[i] + &quot;&#39;&gt;&quot; + data[i] + &quot; &lt;/option&gt;&quot;;
}
$(&#39;#DegreeTitle&#39;).html(items);
},
})
}
&lt;/script&gt;
}

Demo:

ASP.NET Core MVC从单个表格创建级联下拉列表(Razor)

huangapple
  • 本文由 发表于 2023年3月9日 12:37:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定