ASP .net core MVC 中的 ViewModel

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

我试图从控制器调用我的视图模型,但它给我一个错误,SqlException:当它到达我的

@foreach (var item in Model)
语句时,对象名称“OfficerLangVM”无效。我不太熟悉使用视图模型,非常感谢任何帮助。

控制器:

public IActionResult Privacy(string AgencyCity, string AgName, string AgGuid)
{

if (_Db.OfficerLangVMs == null)
{
    return Problem("Entity set 'FLIPV2Context.Officers'  is null.");
}
var officers = from o in _Db.OfficerLangVMs
               select o;
if (!String.IsNullOrEmpty(AgencyCity))
{
    officers = officers.Where(o => o.City!.Contains(AgencyCity));
}
if (!String.IsNullOrEmpty(AgName))
{
    officers = officers.Where(o => o.AgencyName!.Contains(AgName));
}

return View(officers);
}

查看:

@model IEnumerable<CoreApp.ViewModels.OfficerLangVM>
<!--Header-->
@{
    Html.RenderPartial("~/Views/Shared/PageHeader.cshtml");
}
<br />

<!--Administer Agency,FLAgSearch, FLSearchAgency-->
<head runat="server">
    <title>View Agency - Select Agency</title>
</head>
<body>
    <form asp-controller="Agency" asp-action="Privacy">
        <table style="margin-left:-250px" cellpadding="20" border="0">
            <tr>
                <td valign="top" width="375" style="background-color:aliceblue">
                    <font face="Arial">
                        Enter Part of Agency Name:
                    </font>
                    <br />
                    <input type="text" name="AgName" width="250"/>
                    <br />
                    <font>
                        Or Enter City:
                    </font>
                    <br />
                    <input type="text" name="AgencyCity" maxlength="40" width="250"/>
                    <br />
                    <input type="submit" value="submit"/>
                </td>
            </tr>
        </table>
    </form>
</body>

<br />
<tbody>
    <table class="table table-boardered table-striped" style="margin-left:-250px; background-color:lightgrey; width:100%" cellpadding="5">
        <thead>
            <tr>
                <th>@Html.DisplayNameFor(model => model.AgencyName)</th>
                <th>@Html.DisplayNameFor(model => model.City)</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td> @Html.DisplayFor(modelItem => item.AgencyName)</td>
                    <td>@Html.DisplayFor(modelItem => item.City)</td>
                    <td>

                        <a asp-action="Edit" asp-route-id="@item.OfficerKey">Edit Officers</a>
                    </td>
                </tr>
            }

    </table>
</tbody>

视图模型:

public class OfficerLangVM
{

public List<Language> Language;
public Officer Officers;
public string OfficerKey;
public string LanguageKey; 
public string LanguageName;

[Display(Name = "Rank")]
public string OfficerRank;

[Display(Name ="Officer")]
public string FirstName;
public string LastName;

public string Fluency;

[Display(Name ="Agency")]
public string AgencyName;

[Display(Name ="Address")]
public string Address1;
public string City;

[Display(Name ="Zip Code")]
public string PostalCode;

public string State;
public string Phone;
public string Cell;
public string Ext;
public string Country;
public string Email;

[Display(Name ="Can Read")]
public string CanRead;

[Display(Name = "Can Write")]
public string CanWrite;

[Display(Name = "Can Speak")]
public string CanSpeak;

[Display(Name = "Can Understand")]
public string CanUnderstand;

[Display(Name ="Date Modified")]
public DateTime DateUpdated; 
 }
asp.net-mvc asp.net-core controller
1个回答
0
投票

您在视图中将模型定义为

@model IEnumerable<CoreApp.ViewModels.OfficerLangVM>
,因此您发送到视图
officers
中的
return View(officers);
必须是
IEnumerable<OfficerLangVM>

我的意思是我推断你在控制器中定义了变量

officers
,类似于
public IEnumerable<OfficerLangVM> officers
public List<OfficerLangVM> officers
,否则你会得到如下异常:

同时,您提到了

SqlException: Invalid object name 'OfficerLangVM' when it reaches my @foreach (var item in Model) statement
,因此问题应该与您如何查询数据表有关。

此异常通常发生在数据库中没有名为

OfficerLangVM
的表时。这是一个高票答案,它共享相同的例外。

在您的场景中,恐怕您正在尝试按城市名称和机构名称查询官员以及语言技能,因此您可能想要查询官员表而不是

OfficerLangVM
表。这取决于您如何设计表以获得正确的代码。这里我们可以分享的是,如果你现在拥有的View正是你想要的,那么你需要返回一个
OfficerLangVM
的列表,而
List<OfficerLangVM>
中的数据内容可能需要你查询几个表,或根据需要创建数据库视图。如果我们在数据库中有一个官员表,那么
var officers = from o in _Db.Officer select o;
会给我们
IEnumerable<Officer>
作为响应。

这是我的测试:

@model IEnumerable<WebAppMvc2.Models.OfficerLangVM>

@{
    ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>

<table class="table table-boardered table-striped" style="background-color:lightgrey; width:100%" cellpadding="5">
    <thead>
        <tr>
            <th>@Html.DisplayNameFor(model => model.AgencyName)</th>
            <th>@Html.DisplayNameFor(model => model.City)</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td> @Html.DisplayFor(modelItem => item.AgencyName)</td>
                <td>@Html.DisplayFor(modelItem => item.City)</td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.OfficerKey">Edit Officers</a>
                </td>
            </tr>
        }
    </tbody>
</table>

public IActionResult Privacy()
{
    var res = new List<OfficerLangVM> { 
        new OfficerLangVM{
            AgencyName = "agency A",
            City = "city A",
            OfficerKey = "key1"
        },
        new OfficerLangVM{
            AgencyName = "agency B",
            City = "city A",
            OfficerKey = "key2"
        }
    };
    return View(res);
}

namespace WebAppMvc2.Models
{
    public class OfficerLangVM
    {
        public Officer Officers;
        public string OfficerKey;
        [Display(Name = "name of Agency")]
        public string AgencyName;
        [Display(Name = "name of city")]
        public string City;
    }

    public class Officer {
        public int age { get; set; }
        public string name { get; set; }
    }
}

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