简体   繁体   English

ViewBag RuntimeBinderException:“对象”不包含“ProfileId”的定义

[英]ViewBag RuntimeBinderException: 'object' does not contain a definition for 'ProfileId'

I am having trouble passing a list through ViewBag.StaffList to the view to display to the user.我无法通过ViewBag.StaffList将列表传递给视图以显示给用户。 On this particular page, I only need to display 4 out of the 11 columns.在这个特定页面上,我只需要显示 11 列中的 4 列。 I've tried referencing other similar questions from stack overflow and other sites before posting.在发布之前,我尝试过从堆栈溢出和其他网站引用其他类似的问题。 Every time I make a change that appears would work, a different error would appear.每次我做出看起来可行的更改时,都会出现不同的错误。 I am using ASP.NET Core and Entity Framework Core.我正在使用 ASP.NET 核心和实体框架核心。 I am really stuck on this and any help would be greatly appreciated!我真的坚持这一点,任何帮助将不胜感激!

Here is my Controller:这是我的 Controller:

var StaffList = db.StaffProfiles.Where(a => a.Artist).Select(b => new { b.ProfileId, b.FirstName, b.Title, b.ProfilePictureId }).ToList();
ViewBag.StaffList = StaffList;
return View();

Here is my View:这是我的观点:

@foreach (var employee in ViewBag.StaffList)
{
    <div class="col-lg-4 col-md-6 mb30">
        <div class="team-card-default">
            <img src="~/media/images/team/artists/@employee.ProfileId/@employee.ProfilePictureId" alt="@employee.FirstName" class="img-fluid" width="500" height="500">
            <div class="text-center"><h4 class="mb0 text-uppercase">@employee.FirstName</h4></div>
            <div class="text-center"><span>@employee.Title</span></div>
            <div class="text-center">
                <a asp-controller="team" asp-action="profile" asp-route-id="@employee.ProfileId" class="btn btn-lg btn-rounded btn-primary">View My Profile</a>
            </div>
        </div>
    </div>
}

Here is my Model:这是我的 Model:

public class StaffProfilesModel
{
    public string ProfileId { get; set; }
    public bool EmployeeActive { get; set; }
    public bool Artist { get; set; }
    public bool Operations { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Title { get; set; }
    public string ProfilePictureId { get; set; }
    public string SampleOneId { get; set; }
    public string SampleTwoId { get; set; }
    public string SampleThreeId { get; set; }
}

I dont think use of viewmodel required here.我认为这里不需要使用视图模型。 you can directly pass the model through view.您可以通过视图直接传递 model。

return View(stafflist);

And after this, you can try scaffloding for view.在此之后,您可以尝试脚手架观看。

in view:鉴于:

@model List<StaffList>

now现在

 @forech ( var employee in model){

//remaining code } //剩余代码 }

The reason is that you didn't assign the model type when querying the resource, try to modify the LINQ Query code as below:原因是你在查询资源时没有指定model类型,尝试修改LINQ查询代码如下:

        var StaffList = db.StaffProfiles.Where(a => a.Artist)
            .Select(b => new StaffProfilesModel() { 
                ProfileId = b.ProfileId, 
                FirstName= b.FirstName, 
                Title= b.Title, 
                ProfilePictureId = b.ProfilePictureId })
            .ToList();
        ViewBag.StaffList = StaffList;

You could also create a new page model to display the selected properties.您还可以创建一个新页面 model 以显示选定的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Viewbag RuntimeBinderException:“对象”不包含定义 - Viewbag RuntimeBinderException: 'object' does not contain a definition 在视图中提取 ViewBag 动态数据:RuntimeBinderException:“对象”不包含“名称”的定义 - Extract ViewBag Dynamic Data in View : RuntimeBinderException: 'object' does not contain a definition for 'Name' RuntimeBinderException:“对象”不包含“名称”的定义 - RuntimeBinderException: 'object' does not contain a definition for 'Name' RuntimeBinderException:&#39;IReadDataService&#39;不包含&#39;GetBySlug&#39;的定义 - RuntimeBinderException: 'IReadDataService ' does not contain a definition for 'GetBySlug' LINQ加入Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:&#39;对象&#39;不包含以下内容的定义: - LINQ Join Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for “对象”不包含定义 - 'object' does not contain a definition 不包含对象的定义 - Does not contain definition for object “对象”不包含定义 - 'Object' does not contain a definition for 遍历ViewBag时,“字符串”不包含帮助程序的定义 - 'string' does not contain a definition for helper when looping through ViewBag Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“System.__ComObject”不包含“语言”的定义 - Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.__ComObject' does not contain a definition for 'Language''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM