Systems.Collections.Generic.List

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

传入 ViewDataDictionary 的模型项的类型为“System.Collections.Generic.List

1[CoreApp2.Models.Officer]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable
1[CoreApp2.ViewModels.OfficerLangVM]”。

我正在尝试将视图模型添加到我的项目中,但不断收到此错误。我尝试使用视图模型的原因是能够在单个视图上使用多个模型。我的代码如下,不知道哪里出错了。

这是我的控制器:

public IActionResult TestingVM(string LanguageList, string state)
{
    List<Officer> Officers;


    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
    List<Language> Languages;

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

查看型号:

public class OfficerLangVM
{
    public List<Language> Language;
    public Officer Officer;

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

查看:

@using CoreApp2.ViewModels
@model IEnumerable<OfficerLangVM>
@{
    ViewData["Title"] = "FLIP";
}
<html>
<head>
    <title>@ViewData["Title"]</title>
</head>
<hr style="margin-left:-250px;width:100%" />
<body>
    <form id="SearchForm" method="post" action="/Home/TestingVM" style="margin-left:-250px">
        <div>
            <table border="0">
                <tr>
                    <td vAlign="top" width="400" bgColor="aliceblue">
                        <h6>
                            <Font face="Arial" size="2">
                                &nbsp;DIRECTIONS: To find an interpreter . . .
                            </Font>
                            <!-- controls for specifying the required Agency ID or name --><br>
                        </h6>
                        <p>
                            @{
                                Html.RenderPartial("LanguageList.cshtml");
                            }
                            <br />
                            <Font face="Arial" size="2">
                                2) Enter one of the following, if you want to restrict your search:
                            </Font>
                            <table>
                                <tr>
                                    <td align="right">City:&nbsp;</td>
                                    <td>
                                        <input type="text" id="textcity" name="City" maxlength="100" colmuns="20" width="200">
                                    </td>
                                </tr>
                                <tr>

                                    <td align="right">State:&nbsp;</td>
                                    <td>
                                        <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 align="right">Zip:&nbsp;</td>
                                    <td>
                                        <input type="text" id="txtzip" name="PostalCode" maxlength="5" colmuns="20" width="100">
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <button type="submit" id="searchButton">Search</button>
                                    </td>
                                </tr>
                            </table>
                            <td>
                                <FONT face="Arial" size="2">
                                    <p>This program was designed by MAGLOCLEN to assist investigators 
                                    dealing with issues of foreign languages<br>
                                    by providing access to information on law enforcement officers with language
                                <br>
                                    skills.
                                    </p>
                                    <p>To find an interpreter:</p>
                                    <ol>
                                        <li>Select a language from the picklist on the left.</li>
                                        <li>[Optional] Limit your search by an area surrounding your zip code. This will<br>
                                        narrow the list of speakers for the language you selected to those geographically<br>
                                        closest to the zip code you enter.
                                        </li>
                                    </ol>                              
                            
                                    <p>To enter information on yourself or someone in your agency who speaks a language:</p>
                                    <ol>
                                        <li>Click on "Administer Agency" and search for your agency.</li>
                                        <li>
                                            Follow the instructions provided to enter a speaker, language, and contact information
                                        </li>
                                    </ol>  
                                    <p>Thank you for participating in this program. MAGLOCLEN hopes it is useful to you and your agency. 
                                        Please feel free to use the "Contact" tab at the top of this page to provide feedback.
                                    </p>
                                </font>
                            </td>
                        </p>
                    </td>
                </tr>
            </table>

        </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; margin-left:-250px">
           <tr>
               <td>First Name</td>
               <td>Last Name</td>
               <td>Agency</td>
               <td>State</td>
               <td>City</td>
               <td>Fluency</td>

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

            }

        </table>
}
asp.net-mvc .net-6.0 viewmodel
1个回答
0
投票

在您的 TestVM 方法中,您将返回

return View("ViewModel", Officers);
,其中官员为
List<Officer>
,并且视图预计为
IEnumerable<OfficerLangVM>
(
@model IEnumerable<OfficerLangVM>
)。

根据您现有的代码,您可能希望在最后得到类似的内容:

IEnumerable<OfficerLangVM> output = new();
foreach(Officer o in Officers){
  output.add(new OfficerLangVM{
     Officer = o,
     Language = Languages,
     //....any other fields in OfficerLangVM
  });
}
return View("ViewModel", output);
© www.soinside.com 2019 - 2024. All rights reserved.