如何让下拉列表一起工作

问题描述 投票:0回答:1

我正在使用 ASP.NET Core 6 MVC,我需要我的程序有 2 个单独的下拉列表,它们协同工作以显示在所选州讲所选语言的官员。

到目前为止,我已经能够将它们放在同一个视图页面上,并且一旦我单击搜索按钮,它就会“可以搜索”,它只会给我一个空表。我的问题是如何用数据库中的数据填充表格。

控制器:

public IActionResult ViewModels()
{
    return View();
}

[HttpPost]
public IActionResult TestingVM(string state, string LanguageList)
{
    List<OfficerLangVM> output = new();

    if (string.IsNullOrEmpty(state))
    {
        Officers = null;
    }
    else
    {
        IQueryable<Officer> query = _Db.Officers;

        if (!string.IsNullOrEmpty(state))
        {
            query = query.Where(o => o.State == state);
        }

        Officers = query.ToList();
    }

    ViewBag.SelectedState = state;
    ViewBag.States = _Db.Officers.Select(o => o.State).Distinct().ToList();

    ViewBag.SelectedLanguage = LanguageList; // if none is selected, set to control the logic

    if (string.IsNullOrEmpty(LanguageList))
    {
        Languages = new List<Language>();   // to initialize the list 
    }
    else
    {
        Languages = _Db.Languages
                       .Where(s => s.LanguageName == LanguageList)
                       .ToList();
    }

    foreach (Officer o in Officers)
    {
        output.Add(new OfficerLangVM
                       {
                           Officers = o,
                           Language = Languages,
                       });
    }

    return View("ViewModels", output);
}

查看:

@using CoreApp2.ViewModels

@model IEnumerable<OfficerLangVM>

@{
    ViewData["Title"] = "FLIP";
}
<html>
   <body>
      <form id="SearchForm" method="post" action="/Home/TestingVM" style="margin-left:-250px">
          <div>
            <table border="0">
                @{
                   Html.RenderPartial("LanguageList.cshtml");
                 }
                        <select name="State" id="State">
                           <option Value=""> Select a State . . .</option>
                            <option value="DE">Delaware</option>
                            <option value="DC">District of Columbia</option>
                            <option value="IN">Indiana</option>
                            <option value="MD">Maryland</option>
                            <option value="MI">Michigan</option>
                            <option value="NJ">New Jersey</option>
                            <option value="NY">New York</option>
                            <option value="OH">Ohio</option>
                            <option value="PA">Pennsylvania</option>
                            <option value="VA">Virginia</option>
                            <option value="ON">Ontario</option>
                            <option value="QC">Quebec</option>
                        </select>
                    
                                        
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <button type="submit" id="searchButton">Search</button>
                                    </td>
        </div>
    </form>
</body>

@if (Model != null && Model.Any())
{
    <table class="table table-bordered table-striped" style="width:100%;
                  background-color: white; border:1px solid black;">
        <thead>
            <tr>
                <td>First Name</td>
                <td>Last Name</td>
                <td>Agency</td>
                <td>State</td>
                <td>City</td>
                <td>Address</td>
                <td>Language Name</td>
                <td>Fluency</td>
            </tr>
        </thead> 

        @foreach (var officer in Model)
        {
            <tr>
                <td>@officer.FirstName</td>
                <td>@officer.LastName</td>
                <td>@officer.AgencyName</td>
                <td>@officer.City</td>
                <td>@officer.State</td>
                <td>@officer.Address1</td>
                <td>@officer.LanguageName</td>
                <td>@officer.Fluency</td>
            </tr>  
        }
    </table>
}

查看模型:

using CoreApp2.Models;
using CoreApp2.Models.Context;
using Microsoft.AspNetCore.Mvc.Diagnostics;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Security.Policy;

namespace CoreApp2.ViewModels
{
     public class OfficerLangVM
     {
        public List<Language> Language;
        public Officer Officers;
        public string OfficerKey;
        public string LanguageName; 
        public string FirstName;
        public string LastName;
        public string Fluency;
        public string AgencyName;
        public string Address1;
        public string City;
        public string PostalCode;
        public string State;
        public string Phone;
        public string Cell;
        public DateTime DateUpdated; 
   }
}
asp.net-core-mvc .net-6.0
1个回答
0
投票

“有 2 个单独的下拉列表,它们一起向官员显示 谁在选定的州说选定的语言”

1.您的部分观点没有提供,所以我认为是正确的。请设置断点并检查是否运行良好,如果运行不正常请提供代码。

2.您只在OfficerLangVM中填写了Offices中的数据,而没有填写其他属性,因此您得到了一个空表。当你想要获取价值时,你需要联系官员然后获取属性

@foreach (var officer in Model)
{
    <tr>
        <td>@officer.Officers.FirstName</td>
        <td>@officer.Officers.LastName</td>
        <td>@officer.Officers.AgencyName</td>
        <td>@officer.Officers.City</td>
        <td>@officer.Officers.State</td>
        <td>@officer.Officers.Address1</td>
        <td>@officer.Officers.LanguageName</td>
        <td>@officer.Officers.Fluency</td>
    </tr>
}

© www.soinside.com 2019 - 2024. All rights reserved.